想收集同串header

Want To Collect The Same string of header

我有 header 个 sheet 个

'''
+--------------+------------------+----------------+--------------+---------------+
| usa_alaska   | usa_california   | france_paris   | italy_roma   | france_lyon   |
|--------------+------------------+----------------+--------------+---------------|
+--------------+------------------+----------------+--------------+---------------+
'''
df = pd.DataFrame([], columns = 'usa_alaska  usa_california  france_paris  italy_roma  france_lyon'.split())

我想以某种方式按国家和地区分隔 header,这样当我调用 france 时,我应该得到 parislyon 作为列.

根据您的列名创建一个 MultiIndex:

假设这个数据框:

>>> df
   usa_alaska  usa_california  france_paris  italy_roma  france_lyon
0           1               2             3           4            5
df.columns = df.columns.str.split('_', expand=True)
df = df.sort_index(axis=1)

输出

>>> df
  france       italy    usa           
    lyon paris  roma alaska california
0      5     3     4      1          2

>>> df['france']
   paris  lyon
0      3     5