I've created an example how I usually do same tasks:
df = pd.DataFrame({'a': [1,1,1], 'b': [1,2,3], 'c': [0,-1,-2]})
print(df)
Out:
a b c
0 1 1 0
1 1 2 -1
2 1 3 -2
This is sample dataframe. You can apply some function to modify all it's columns depending on column's name, available throgh name
attribute. For example, column 'a'
has just name a
:
df['a'].name
Out:
'a'
def mapper(name):
return name + '_new'
df_new = df.apply(lambda col: col * 4 if col.name == 'a' or col.name == 'b'
else col * 7 if col.name == 'CZK_fwdp'
else col * 5, axis=0).rename(mapper=mapper, axis=1)
print(df_new)
Out:
a_new b_new c_new
0 4 4 0
1 4 8 -5
2 4 12 -10
Use apply
method to apply you lambda function along the columns with argument axis=1. Function mapper
is used to avoid column's names interference. To get desired dataframe, you can just concatente old and new dataframes:
df_conc = pd.concat((df, df_new), axis=1)
print(df_conc)
Out:
a b c a_new b_new c_new
0 1 1 0 4 4 0
1 1 2 -1 4 8 -5
2 1 3 -2 4 12 -10
If you don't like lambda functions, you can use common function:
def modify_column(col):
if col.name == 'a' or col.name == 'b':
return col * 4
elif col.name == 'CZK_fwdp':
return col * 7
else:
return col * 5
df_new = df.apply(modify_column, axis=0).rename(mapper=mapper, axis=1)