在你有一個柱狀多指標的特殊情況,但一個簡單的指標,您可以移調數據框,並使用index_label
和index_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.save
和pd.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)
這是一個懸而未決的問題:https://github.com/pydata/pandas/issues/1651 – Jeff 2013-05-05 22:38:17