I have a dataframe df with three columns. Let's say the columns are "A", "B" and "C". And, I have three different functions func1, func2 and func3 which needs to be applied on column A, B and C respectively.
func1 -> column A, func2 -> column, B func3 -> column C
df["A"].apply(lambda x: func1(x))
df["B"].apply(lambda x: func2(x))
df["C"].apply(lambda x: func3(x))
Here, I have to call three apply method on the three different columns. Is there any way to call the apply method once and run three different functions to three different columns instead of calling the apply method three times?
CodePudding user response:
Assuming your function returns a value for each row:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3], 'B':[1,2,3], 'C':[1,2,3]})
df.transform({
'A':lambda x: x-1,
'B': lambda x: x 1,
'C': lambda x: x*2
})
Output
A B C
0 0 2 2
1 1 3 4
2 2 4 6