【问题标题】:Not able to store the multi-index csv file using pandas无法使用熊猫存储多索引 csv 文件
【发布时间】:2019-11-25 05:44:59
【问题描述】:

我有一个看起来像的数据框,

                         JAPE_feature                     
                     100 200 2200 2600 4600         
did offset word                                                               
0   0      aa          0   1    0    0    0          
0   11     bf          0   1    0    0    0           
0   12     vf          0   1    0    0    0             
0   13     rw          1   0    0    0    0             
0   14     asd         1   0    0    0    0               
0   16     dsdd        0   0    1    0    0               
0   18     wd          0   0    0    1    0              
0   20     wsw         0   0    0    1    0               
0   21     sd          0   0    0    0    1

现在,我正在尝试将此数据帧保存为 csv 格式。

df.to_csv('data.csv')

所以,它会像这样存储,

现在,我在这里尝试保存而不在 JAPE_feature 列中创建新列。它只会在一列中包含 5 个子功能。

         JAPE_FEATURES
   100 |  200 |  2200 |   2600 | 4600 

the sub-columns should be like this . It should not create the different columns 

【问题讨论】:

  • 预期输出如何?
  • 我只会补充一点,.. 它不会有差异列。因此,JAPE_feature 将是第一列,但子列应该在同一列中
  • 以这种方式保存的原因是什么?因为我认为你只能在exceldf.to_excel('data.xlsx')中做到这一点,在csv中默认是不可能的。
  • 好的..我会尝试这样做..使用 xlsx 格式。然后将再次检查..

标签: python python-3.x pandas


【解决方案1】:

我认为这里最好的是将DataFrame转换为excel,如果需要merge一级MultiIndex in columns

df.to_excel('data.xlsx')

如果想要 csv 那就有问题了,需要更改 MultiIndex 以将重复值替换为空字符串:

print (df.columns)
MultiIndex([('JAPE_feature',  100),
            ('JAPE_feature',  200),
            ('JAPE_feature', 2200),
            ('JAPE_feature', 2600),
            ('JAPE_feature', 4600)],
           )

cols = df.columns.to_frame()
cols[0] = cols[0].mask(cols[0].duplicated(), '')
df.columns = pd.MultiIndex.from_arrays([cols[0], cols[1]])
print (df.columns)
MultiIndex([('JAPE_feature',  100),
            (            '',  200),
            (            '', 2200),
            (            '', 2600),
            (            '', 4600)],
           names=[0, 1])

df.to_csv('data.csv')

【讨论】:

  • 很好的答案@jezrael.. 一如既往:-)
猜你喜欢
  • 1970-01-01
  • 2017-05-01
  • 2023-02-01
  • 1970-01-01
  • 2019-05-07
  • 2016-10-16
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多