Python 熊猫:如何将行中的一列转为列

Python 熊猫:如何将行中的一列转为列,python,pandas,dataframe,pivot,multiple-columns,Python,Pandas,Dataframe,Pivot,Multiple Columns,给定此数据帧: 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

给定此数据帧:

   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
功能仅限于(
a
b
c

我想将数据帧转为以下内容:

   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
   ...
   ...
您可以使用:

最后,您可以通过删除列名称(在
pandas
0.18.0
中新增):

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
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