Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 648
sapo_cosmico Avatar asked May 04 '17 11:05

sapo_cosmico


People also ask

How do I change the index of a DataFrame in pandas?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

How do you replace an index in a data frame?

Pandas DataFrame: set_index() function The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.

How do you switch columns and indexes in Python?

Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object but returns a new object with the rows and columns swapped (= transposed object).

How do I change the index of a DataFrame to another column?

You can change the index to a different column by using set_index() after reset_index() .


1 Answers

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
like image 106
jezrael Avatar answered Sep 23 '22 16:09

jezrael