I have a pandas Dataframe and i want to group some of the columns to build higher levels columns:
Exemple : i have
Index A B C D
1 0.25 0.3 0.25 0.66
2 0.25 0.3 0.25 0.66
3 0.25 0.3 0.25 0.66
and i want
Index AB || CD
Subindex A | B || C | D
1 0.25 | 0.3 || 0.25 | 0.66
2 0.25 | 0.3 || 0.25 | 0.66
3 0.25 | 0.3 || 0.25 | 0.66
Thank you for your help...
Create a dictionary to define your mapping and use pd.MultiIndex.from_tuples
. If needed you can also specify names=['level_0', 'level_1']
to add names.
import pandas as pd
d = {'A': 'AB', 'B': 'AB', 'C': 'CD', 'D': 'CD'}
df.columns = pd.MultiIndex.from_tuples([*zip(map(d.get, df), df)])
# Equivalently
# df.columns = pd.MultiIndex.from_tuples([(d[col], col) for col in df.columns])
Output:
AB CD
A B C D
Index
1 0.25 0.3 0.25 0.66
2 0.25 0.3 0.25 0.66
3 0.25 0.3 0.25 0.66
groupby
/ concat
hack m = {'A': 'AB', 'B': 'AB', 'C': 'CD', 'D': 'CD'}
pd.concat(dict((*df.groupby(m, 1),)), axis=1)
AB CD
A B C D
Index
1 0.25 0.3 0.25 0.66
2 0.25 0.3 0.25 0.66
3 0.25 0.3 0.25 0.66
Note that with this method it is possible to select an arbitrary subset of the columns in the original DataFrame, whereas the alternative answer appears to require a valid dictionary mapping for all values in the parent DataFrame
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.