用multiindex连接多个.csv数据框

I am concatenating multiple dfs which look like these:

                 X                  Y
                 mean   std size   mean         std  size
In_X                    
(10.424, 10.43] 10.425  NaN  1      0.003786    NaN   1
(10.43, 10.435] 10.4    NaN  0      NaN         NaN   0

When I didn't have multiindex dfs, I was using:

extension='csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
all_dfs = pd.concat([pd.read_csv(f) for f in all_filenames ])

但这会引起一行:

mean   std size   mean          std  size

Every time a new df is concatenated to all_dfs. How to have only the original multiindex header and avoid the introduction of the second-level header in the concatenated df?

评论
  • taut
    taut 回复

    read_csv by defaults only take first row as header. You want to do specify two-row header with header:

    all_dfs = pd.concat([pd.read_csv(f, header=[0,1] for f in all_filenames ])
    

  • 观世音菩萨
    观世音菩萨 回复

    将您的多索引转换为常规列,如下所示:

    df.columns = df.columns.map('_'.join)
    

    And then use pd.concat