'pandas rename columns with method chaining
I have a dataframe and did some feature engineering and now would like to change the column names.
I know how to change them if I do a new assignment but I would like to do it with method chaining. I tried the below (the rename row) but it doesn't work. How could I write it so it works?
df = pd.DataFrame({'ID':[1,2,2,3,3,3], 'date': ['2021-10-12','2021-10-16','2021-10-15','2021-10-10','2021-10-19','2021-10-01'],
'location':['up','up','down','up','up','down'],
'code':[False, False, False, True, False, False]})
df = (df
.assign(date = lambda x: pd.to_datetime(x.date))
.assign(entries_per_ID = lambda x: x.groupby('ID').ID.transform('size'))
.pivot_table(values=['entries_per_ID'], index=['ID','date','code'],
columns=['location'], aggfunc=np.max)
.reset_index()
#.rename(columns=lambda x: dict(zip(x.columns, ['_'.join(col).strip() if col[1]!='' else col[0] for col in x.columns.values])))
)
This here works, but that's not how I would like to write it.
df.columns = ['_'.join(col).strip() if col[1]!='' else col[0] for col in df.columns.values ]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
