I would like to modify multiple columns with pandas.
import pandas as pd
df = pd.DataFrame({'a': [1,2], 'b':[3,4]})
df = df.assign(**{v: lambda x:x[v]*10 for v in ['a', 'b']})
doesn't return the expected result.
10 30
20 40
It returns
30 30
40 40
To my understanding this is due to lazy binding, explained here: https://sopython.com/wiki/Common_Gotchas_In_Python
The desired result can be obtained by a proper function.
def fun(x):
return x*10
df[['a','b']] = df[['a','b']].apply(fun)
Is there a way to break the lazy binding in a dictionary comprehension like above? Or are lambda functions too limited ?