Python Forum

Full Version: How can I write a function with three parameters?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
What have you tried?
(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?