Python Forum
How can I write a function with three parameters? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How can I write a function with three parameters? (/thread-32767.html)



How can I write a function with three parameters? - MehmetAliKarabulut - Mar-04-2021

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?


RE: How can I write a function with three parameters? - buran - Mar-04-2021

What have you tried?


RE: How can I write a function with three parameters? - MehmetAliKarabulut - Mar-04-2021

(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)



RE: How can I write a function with three parameters? - buran - Mar-04-2021

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?