如何导出和导入具有多索引行和列索引 into/from 的 pandas 数据框 csv 文件?

How can I export and import a pandas dataframe with multiindex row and column index into/from a csv file?

Pandas 允许使用 to_csv('path/to/file.csv'). However to me it is not clear how I can export (and import) a dataframe which uses MultiIndex for rows and columns like e.g. this one from the corresponding advanced docs:

将数据帧导出到 csv
first              bar                 baz                 foo          
second             one       two       one       two       one       two
first second                                                            
bar   one    -0.410001 -0.078638  0.545952 -1.219217 -1.226825  0.769804
      two    -1.281247 -0.727707 -0.121306 -0.097883  0.695775  0.341734
baz   one     0.959726 -1.110336 -0.619976  0.149748 -0.732339  0.687738
      two     0.176444  0.403310 -0.154951  0.301624 -2.179861 -1.369849
foo   one    -0.954208  1.462696 -1.743161 -0.826591 -0.345352  1.314232
      two     0.690579  0.995761  2.396780  0.014871  3.357427 -0.317441

要在 Jupyter notebook 中使用随机数据值生成这样的数据帧:

import pandas as pd
import numpy as np

column_index_matrix = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo']),
                       np.array(['one', 'two', 'one', 'two', 'one', 'two'])]
column_names = ['first', 'second']
column_multiindex = pd.MultiIndex.from_arrays(column_index_matrix, names=column_names)
column_multiindex
row_multiindex = column_multiindex
df = pd.DataFrame(np.random.randn(6, 6), index=row_multiindex, columns=column_multiindex)
df

WHen 运行 df.to_csv(r'df.csv', index=True) 数据框正确导出到 csv 文件。但是我不知道如何使用 pd.read_csv().

我之前尝试过,但最终认为最好是:

  • 拆散整个列多索引
  • reset_index
  • to_csv

然后:

  • read_csv
  • pivot_table

看起来比较简单,但可能占用更多内存。

您可以传递多个 columns/rows 用作 index/header。在这里,我传递用于索引的第 0 列和第 1 列(因此两个级别)以及用于两个 header 级别的第 0 行和第 1 行:

pd.read_csv('data.csv', index_col=[0, 1], header=[0, 1])
first              bar                 baz                 foo          
second             one       two       one       two       one       two
first second                                                            
bar   one     0.788793 -0.591498 -0.309037 -0.433105 -1.413536 -0.209560
      two    -0.354429  1.671837  1.527225  0.282775 -0.973088 -0.728555
baz   one    -0.180517  1.226219 -0.810984 -0.580251 -0.453205 -1.368015
      two    -0.040708 -0.836359 -2.043332  1.396955 -0.562718 -1.099926
foo   one    -0.612561  0.815998 -0.942997 -0.423395  0.157410 -0.537063
      two    -0.312878  0.194915 -1.420048 -0.944414 -0.560043 -0.036713