I do have a dataframe df
with several columns like this:
col1 col2
0 0.627521 0.026832
1 0.470450 0.319736
2 0.015760 0.484664
3 0.645810 0.733688
4 0.850554 0.506945
I want to apply a function to each of these columns and add the results as additional columns (similar to this question) whereby the names are the original names plus a common suffix for all added columns.
I tried the following (highly simplified case):
import pandas as pd
import numpy as np
def do_and_rename(s, s2):
news = s + s2
news.name = s.name + "_change"
return news
df = pd.DataFrame({'col1': np.random.rand(5), 'col2': np.random.rand(5)})
new_df = pd.concat([df, df.apply(lambda x: do_and_rename(x, df.index))], axis=1)
which gives me
col1 col2 col1 col2
0 0.627521 0.026832 0.627521 0.026832
1 0.470450 0.319736 1.470450 1.319736
2 0.015760 0.484664 2.015760 2.484664
3 0.645810 0.733688 3.645810 3.733688
4 0.850554 0.506945 4.850554 4.506945
The calculations are correct but the column names are wrong.
My desired output would be
col1 col2 col1_change col2_change
0 0.627521 0.026832 0.627521 0.026832
1 0.470450 0.319736 1.470450 1.319736
2 0.015760 0.484664 2.015760 2.484664
3 0.645810 0.733688 3.645810 3.733688
4 0.850554 0.506945 4.850554 4.506945
If I just do
do_and_rename(df['col1'], df.index)
I get
0 0.627521
1 1.470450
2 2.015760
3 3.645810
4 4.850554
Name: col1_change, dtype: float64
with the correct name. How can I use these returned names as columns headers?