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
...
...
We can use str. split() to split one column to multiple columns by specifying expand=True option.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With