sapo_cosmico
sapo_cosmico

Reputation: 6524

How to swap index and values on 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 the tnr, of the threshold that corresponds to that tnr, for each class. Essentially, I want this:

enter image description here

I am able to achieve this effect by 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 hacky and error prone. Is there a better way to do this, and is there maybe native Pandas functionality that would do this?

Upvotes: 7

Views: 14425

Answers (1)

jezrael
jezrael

Reputation: 862671

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

Upvotes: 3

Related Questions