2013-05-05 121 views
3

是否有可能通過csv以一種尊重分層列結構的方式對DataFrame進行往返?換句話說,如果我有以下數據框:熊貓分層列和csv函數

>>> cols = pd.MultiIndex.from_arrays([["foo", "foo", "bar", "bar"], 
             ["a", "b", "c", "d"]]) 
>>> df = pd.DataFrame(np.random.randn(5, 4), index=range(5), columns=cols) 

執行以下操作失敗:

>>> df.to_csv("df.csv", index_label="index") 
>>> df_new = pd.read_csv("df.csv", index_col="index") 
>>> assert df.columns == df_new.columns 

我缺少的CSV保存/讀取步驟的一些選項?

+1

這是一個懸而未決的問題:https://github.com/pydata/pandas/issues/1651 – Jeff 2013-05-05 22:38:17

回答

1

在你有一個柱狀多指標的特殊情況,但一個簡單的指標,您可以移調數據框,並使用index_labelindex_col如下:

import numpy as np 
import pandas as pd 

cols = pd.MultiIndex.from_arrays([["foo", "foo", "bar", "bar"], 
            ["a", "b", "c", "d"]]) 

df = pd.DataFrame(np.random.randn(5, 4), index=range(5), columns=cols) 

(df.T).to_csv('/tmp/df.csv', index_label=['first','second']) 
df_new = pd.read_csv('/tmp/df.csv', index_col=['first','second']).T 
assert np.all(df.columns.values == df_new.columns.values) 

可惜這引出了一個問題做什麼,如果索引和列都是MultiIndexes?


這裏是一個哈克解決方法:

import numpy as np 
import pandas as pd 
import ast 

cols = pd.MultiIndex.from_arrays([["foo", "foo", "bar", "bar"], 
            ["a", "b", "c", "d"]]) 

df = pd.DataFrame(np.random.randn(5, 4), index=range(5), columns=cols) 
print(df) 

df.to_csv('/tmp/df.csv', index_label='index') 
df_new = pd.read_csv('/tmp/df.csv', index_col='index') 

columns = pd.MultiIndex.from_tuples([ast.literal_eval(item) for item in df_new.columns]) 
df_new.columns = columns 
df_new.index.name = None 
print(df_new) 
assert np.all(df.columns.values == df_new.columns.values) 

當然,如果你只是想將數據幀存儲任意格式的文件,然後df.savepd.load提供更舒適的解決方案:

import numpy as np 
import pandas as pd 

cols = pd.MultiIndex.from_arrays([["foo", "foo", "bar", "bar"], 
            ["a", "b", "c", "d"]]) 

df = pd.DataFrame(np.random.randn(5, 4), index=range(5), columns=cols) 

df.save('/tmp/df.df') 
df_new = pd.load('/tmp/df.df') 
assert np.all(df.columns.values == df_new.columns.values)