python - How to store the name of rows and column index in pandas DataFrame? -
i have dataframe named rows , columns indexes:
import numpy np import pandas pd = 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) i have tried store in several formats (excel, csv) when re-reading file, names lost (maybe have missed options). msgpack works marked experimental prefer avoid now. prefer avoid pickle. there way (format , option) store name of 2 indexes?
edit: know how write , read csv pandas. problem save name of column index , of row index.
you can use hdf.
import numpy np import pandas pd = 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 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 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
Comments
Post a Comment