Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How to pivot one column in rows into columns

Given this dataframe:

   feature score    searchTerm
0   a      0.534509 pizza
1   b      0.586020 pizza
2   c      0.588972 pizza
3   a      0.566261 chinese
4   b      0.572405 chinese
5   c      0.489369 chinese
6   a      0.499068 thai
7   b      0.431068 thai
8   c      0.441617 thai

Feature is limited to (a,b,c)

I want to pivot the dataframe into this:

   a        b        c          searchTerm
   0.534509 0.586020 0.588972   pizza
   0.566261 0.572405 0.489369   chinese    
   0.499068 0.431068 0.441617   thai
   ...
   ...
like image 725
samol Avatar asked Jun 28 '16 05:06

samol


People also ask

How do I convert one column to multiple columns in pandas?

We can use str. split() to split one column to multiple columns by specifying expand=True option.

How do I convert rows to columns in pandas?

Pandas DataFrame: transpose() function The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.

How do you convert rows into columns and columns into rows in pandas?

Pandas DataFrame. transpose() is a library function that transpose index and columns. The transpose reflects the DataFrame over its main diagonal by writing rows as columns and vice-versa. Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of DataFrame.

What does reshape do in pandas?

In Pandas data reshaping means the transformation of the structure of a table or vector (i.e. DataFrame or Series) to make it suitable for further analysis. Some of Pandas reshaping capabilities do not readily exist in other environments (e.g. SQL or bare bone R) and can be tricky for a beginner.


1 Answers

You can use pivot:

df1 = df.pivot(index='searchTerm', columns='feature', values='score').reset_index()
print (df1)
feature searchTerm         a         b         c
0          chinese  0.566261  0.572405  0.489369
1            pizza  0.534509  0.586020  0.588972
2             thai  0.499068  0.431068  0.441617

Last you can remove columns name by rename_axis (new in pandas 0.18.0):

df1 = df1.rename_axis(None, axis=1)
#pandas bellow 0.18.0
#df.columns.name = None
print (df1)
  searchTerm         a         b         c
0    chinese  0.566261  0.572405  0.489369
1      pizza  0.534509  0.586020  0.588972
2       thai  0.499068  0.431068  0.441617
like image 94
jezrael Avatar answered Oct 04 '22 20:10

jezrael