我认为这可以做到
In [3]: df = Dataframe(dict(A = 'foo', B = 'bar', value = 1),index=range(5)).set_index(['A','B'])In [4]: dfOut[4]: valueA B foo bar 1 bar 1 bar 1 bar 1 bar 1In [5]: df.to_csv('test.csv')In [6]: !cat test.csvA,B,valuefoo,bar,1foo,bar,1foo,bar,1foo,bar,1foo,bar,1In [7]: pd.read_csv('test.csv',index_col=[0,1])Out[7]: valueA B foo bar 1 bar 1 bar 1 bar 1 bar 1
使用索引重复来编写(虽然有点骇人听闻)
In [27]: x = df.reset_index()In [28]: mask = df.index.to_series().duplicated()In [29]: maskOut[29]: A B foo bar False bar True bar True bar True bar Truedtype: boolIn [30]: x.loc[mask.values,['A','B']] = ''In [31]: xOut[31]: A B value0 foo bar 11 12 13 14 1In [32]: x.to_csv('test.csv')In [33]: !cat test.csv,A,B,value0,foo,bar,11,,,12,,,13,,,14,,,1
回读实际上有点棘手
In [37]: pd.read_csv('test.csv',index_col=0).ffill().set_index(['A','B'])Out[37]: valueA B foo bar 1 bar 1 bar 1 bar 1 bar 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)