I want to write a three parameter function in Python. The output of the function should be data frame and it should square the given column. For example math_sq (df, column, value). How can I write this function?
(Mar-04-2021, 04:19 PM)buran Wrote: [ -> ]What have you tried?
I tried this below. But it didn't work.
apply_math_sq(df,column,value):
df[["col"]] = df[["col"]].pow(2)
you miss
def
. then there is problem how you access the column.
import pandas as pd
def apply_power_sq(df, column):
df[column] = df[column].pow(2)
df = pd.DataFrame({'spam': [0, 3, 4],
'eggs': [1, 2, 3]})
print(df)
apply_power_sq(df, 'spam')
print(df)
Output:
spam eggs
0 0 1
1 3 2
2 4 3
spam eggs
0 0 1
1 9 2
2 16 3
Now, what is the purpose of value that you want to pass?