Pandas - 使用 to_csv 写入多索引行 [英] Pandas - write Multiindex rows with to_csv

查看:136
本文介绍了Pandas - 使用 to_csv 写入多索引行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 to_csv 将多索引数据帧写入 csv 文件.csv 文件有一列包含元组中的多索引,例如:

I am using to_csv to write a Multiindex DataFrame to csv files. The csv file has one column that contains the multiindexes in tuples, like:

('a', 'x')
('a', 'y')
('a', 'z')
('b', 'x')
('b', 'y')
('b', 'z')

但是,我希望能够将 Multiindex 输出到两列而不是一列元组,例如:

However, I want to be able to output the Multiindex to two columns instead of one column of tuples, such as:

a, x
 , y
 , z
b, x
 , y
 , z

看起来 tupleize_cols 可以为列实现这一点,但对于行则没有这样的选项.有没有办法做到这一点?

It looks like tupleize_cols can achieve this for columns, but there is no such option for the rows. Is there a way to achieve this?

推荐答案

我认为这样做

In [3]: df = DataFrame(dict(A = 'foo', B = 'bar', value = 1),index=range(5)).set_index(['A','B'])

In [4]: df
Out[4]: 
         value
A   B         
foo bar      1
    bar      1
    bar      1
    bar      1
    bar      1

In [5]: df.to_csv('test.csv')

In [6]: !cat test.csv
A,B,value
foo,bar,1
foo,bar,1
foo,bar,1
foo,bar,1
foo,bar,1

In [7]: pd.read_csv('test.csv',index_col=[0,1])
Out[7]: 
         value
A   B         
foo bar      1
    bar      1
    bar      1
    bar      1
    bar      1

用索引重复来写(虽然有点黑客)

To write with the index duplication (kind of a hack though)

In [27]: x = df.reset_index()

In [28]: mask = df.index.to_series().duplicated()

In [29]: mask
Out[29]: 
A    B  
foo  bar    False
     bar     True
     bar     True
     bar     True
     bar     True
dtype: bool

In [30]: x.loc[mask.values,['A','B']] = ''

In [31]: x
Out[31]: 
     A    B  value
0  foo  bar      1
1                1
2                1
3                1
4                1

In [32]: x.to_csv('test.csv')

In [33]: !cat test.csv
,A,B,value
0,foo,bar,1
1,,,1
2,,,1
3,,,1
4,,,1

回读实际上有点棘手

In [37]: pd.read_csv('test.csv',index_col=0).ffill().set_index(['A','B'])
Out[37]: 
         value
A   B         
foo bar      1
    bar      1
    bar      1
    bar      1
    bar      1

这篇关于Pandas - 使用 to_csv 写入多索引行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆