Python Forum
How can I write a function with three parameters?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I write a function with three parameters?
#1
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?
Reply
#2
What have you tried?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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)
buran write Mar-04-2021, 05:17 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#4
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to Write a function with Pandas? SuperNinja3I3 4 2,281 Jul-03-2022, 02:19 PM
Last Post: ndc85430
  Write function to find decryptionkey kotter 3 3,055 Dec-11-2019, 07:04 PM
Last Post: nilamo
  Writing a function that accepts two integer parameters (lines and cheers) taydeal20 1 3,072 Feb-05-2018, 08:35 PM
Last Post: nilamo
  how to write a function in python that solves an equation samiraheen 5 9,691 Sep-28-2017, 01:10 AM
Last Post: Mekire
  Write a function sorting(L)? MoIbrahim 11 8,147 Apr-22-2017, 11:45 AM
Last Post: idontreallywolf
  function to write the contents of a webpage into a new file roadrage 4 5,959 Dec-01-2016, 09:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020