How to replace index and values ​​with pandas dataframe

I have some data in which the index is a threshold and the values ​​are trns (true negative rates) for two classes: 0 and 1.

enter image description here

I want to get a dataframe indexed by tnr, the threshold that matches this tnr, for each class. Essentially, I want this:

enter image description here

I can achieve this effect using the following:

pd.concat([pd.Series(data[0].index.values, index=data[0]), pd.Series(data[1].index.values, index=data[1])], axis=1) 

Or, generalizing to any number of columns:

 def invert_dataframe(df): return pd.concat([pd.Series(df[col].index.values, index=df[col]) for col in df.columns], axis=1) 

However, this seems extremely hacked and error prone. Is there a better way to do this, and maybe there is native Pandas functionality that will do this?

+5
source share
1 answer

You can use stack with pivot :

 data = pd.DataFrame({0:[10,20,31],10:[4,22,36], 1:[7,5,6]}, index=[2.1,1.07,2.13]) print (data) 0 1 10 2.10 10 7 4 1.07 20 5 22 2.13 31 6 36 df = data.stack().reset_index() df.columns = list('abc') df = df.pivot(index='c', columns='b', values='a') print (df) b 0 1 10 c 4 NaN NaN 2.10 5 NaN 1.07 NaN 6 NaN 2.13 NaN 7 NaN 2.10 NaN 10 2.10 NaN NaN 20 1.07 NaN NaN 22 NaN NaN 1.07 31 2.13 NaN NaN 36 NaN NaN 2.13 
+1
source

All Articles