pandas以所需格式打印数据帧

时间:2017-06-13 05:03:09

标签: python pandas dataframe

我有这种形式的数据:

Sample  Cohort  CName  Intensity
S1      a       C1     22.34
S2      a       C2     17.34

我想以这种形式打印

Cohort Intensity1 Intensity2
a      22.34      17.34 

请建议如何操作。我是熊猫的初学者

1 个答案:

答案 0 :(得分:0)

我认为你需要pivot

df = df.pivot(index='Cohort', columns='Sample', values='Intensity')
print (df)
Sample     S1     S2
Cohort              
a       22.34  17.34

或者:

df = df.pivot(index='Cohort', columns='CName', values='Intensity')
print (df)
CName      C1     C2
Cohort              
a       22.34  17.34

最后:

df.columns = ['Intensity' + str(x + 1) for x in np.arange(len(df.columns))]
print (df)
        Intensity1  Intensity2
Cohort                        
a            22.34       17.34

但也许需要:

print (df)
  Sample Cohort CName  Intensity
0     S1      a    C1      22.34
1     S2      a    C2      17.34
2     S1      b    C1      20.00
3     S1      b    C1      10.00

df['g'] = df.groupby('Cohort').cumcount()
df = df.pivot(index='Cohort', columns='g', values='Intensity')
print (df)
g           0      1
Cohort              
a       22.34  17.34
b       20.00  10.00

与:

相同
df = pd.pivot(index=df['Cohort'],
              columns=df.groupby('Cohort').cumcount(),
              values=df['Intensity'])
print (df)
            0      1
Cohort              
a       22.34  17.34
b       20.00  10.00

替代:

df['g'] = df.groupby('Cohort').cumcount()
df = df.set_index(['Cohort', 'g'])['Intensity'].unstack(fill_value=0)
print (df)
g           0      1
Cohort              
a       22.34  17.34
b       20.00  10.00