如何通过数据帧输出重新排列pandas group

时间:2018-01-15 11:00:55

标签: pandas pandas-groupby

ID Sequence    float           Freq     Count
3631 D          1.31              1        1
     P          1.45              1        1
     R          1.44              1        1
3633 D          1.26              3        3
                1.27              2        2
                1.32              1        1
     P          1.33              4        4

以上是pandas groupby的输出

final_df =  small_df.groupby(['ID','Seq','float'])['ID','Seq'].count()

我想把它写成csv文件

3631,"D,P,R","1.31,1.45,1.44"
3633,"D,P","1.26,1.27,1.32,1.33"

希望在这项研究工作中提供一些帮助。 谢谢

1 个答案:

答案 0 :(得分:1)

此问题的本质只是ID上的分组操作,然后是agg str.join注册。

df.reset_index(level=1)\               # reset the first level
  .iloc[:, :2]\                        # select only the first 2 columns
  .astype(str)\                        # convert to string
  .groupby(level=0)\                   # group by the index
  .agg(','.join)\                      # join elements 
  .to_csv('file.csv', quotechar='"')   # save to CSV with a quoting character

file.csv

ID,Sequence,float
3631,"D,P,R","1.31,1.45,1.44"
3633,"D,D,D,P","1.26,1.27,1.32,1.33"