Say I have a pandas.DataFrame
with a hierarchical index on the columns as follows:
假设我有一个pandas.DataFrame,列上有分层索引,如下所示:
import pandas as pd
columns = pd.MultiIndex.from_product([list('AB'), list('ab')])
df = pd.DataFrame(np.arange(8).reshape((2,4)), columns=columns)
print df
Out[1]:
A B
a b a b
0 0 1 2 3
1 4 5 6 7
I would like to flatten the column index so it looks as follows:
我想展平列索引,使其如下所示:
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
I tried
def flatten(col):
col.name = ''.join(col.name)
return col
df.apply(f)
but that just ignored the modified name of the new columns.
但是这只是忽略了新列的修改名称。
3 个解决方案
#1
13
use map
df.columns = df.columns.map(''.join)
df
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
use rename
df.rename(columns=''.join)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
#2
3
You can use list comprehension
with join
:
您可以使用列表理解和连接:
df.columns = [''.join(col) for col in df.columns]
print (df)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
Another possible solution:
另一种可能的方案
df.columns = df.columns.to_series().str.join('')
print (df)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
#3
0
The following works but creates a new DataFrame
:
以下工作但创建一个新的DataFrame:
df_flat = pd.DataFrame({''.join(k):v for k,v in df.iteritems()})
print df_flat
Out[3]:
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
#1
13
use map
df.columns = df.columns.map(''.join)
df
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
use rename
df.rename(columns=''.join)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
#2
3
You can use list comprehension
with join
:
您可以使用列表理解和连接:
df.columns = [''.join(col) for col in df.columns]
print (df)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
Another possible solution:
另一种可能的方案
df.columns = df.columns.to_series().str.join('')
print (df)
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7
#3
0
The following works but creates a new DataFrame
:
以下工作但创建一个新的DataFrame:
df_flat = pd.DataFrame({''.join(k):v for k,v in df.iteritems()})
print df_flat
Out[3]:
Aa Ab Ba Bb
0 0 1 2 3
1 4 5 6 7