如何在pandas DataFrame中存储行和列索引的名称?

Mat*_*ois 5 python pandas

我有一个DataFrame命名的行和列的索引:

import numpy as np
import pandas as pd

I = pd.Index(["a", "b", "c", "d"], name="rows")
C = pd.Index(["col0", "col1", "col2"], name="cols")
df = pd.DataFrame(data=np.random.rand(4, 3),
                  index=I,
                  columns=C)
Run Code Online (Sandbox Code Playgroud)

我试图以多种格式(Excel,CSV)存储它,但是当重新读取文件时,名称会丢失(可能我错过了一些选项).Msgpack有效,但它被标记为实验,所以我宁愿现在避免它.我也想避免pickle.有没有办法(格式和选项)来存储2个索引的名称?

编辑: 我知道如何用pandas写和读CSV.问题是保存列索引和行索引的名称.

Sto*_*ica 6

你可以使用hdf.

import numpy as np
import pandas as pd
I = pd.Index(["a", "b", "c", "d"], name="rows")
C = pd.Index(["col0", "col1", "col2"], name="columns")
df = pd.DataFrame(data=np.random.rand(4,3), index=I, columns=C)
print(df)

columns      col0      col1      col2
rows                                 
a        0.098497  0.918954  0.642800
b        0.168266  0.678434  0.455059
c        0.434939  0.244027  0.599400
d        0.877356  0.053085  0.182661

df.to_hdf('test.hdf', 'test')
print(pd.read_hdf('test.hdf'))

columns      col0      col1      col2
rows                                 
a        0.098497  0.918954  0.642800
b        0.168266  0.678434  0.455059
c        0.434939  0.244027  0.599400
d        0.877356  0.053085  0.182661
Run Code Online (Sandbox Code Playgroud)