Python Forum
how to apply user defined function to Pandas DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to apply user defined function to Pandas DataFrame
i made a user defined function with 4 arguements. these 4 arguements are the 4 columns of the panda dataframe. how do i apply it? i wish to make a new column to store all the return values from the user defined function.

this is my user defined function:
cash_flow_list = []
def all_cash_flows (fv, cp_rate, maturity_year, coupon_per_year):
    for i in range (1,maturity_year*coupon_per_year + 1):
        if i == (maturity_year*coupon_per_year):
            cash_flow_list.append((fv*cp_rate/coupon_per_year) + fv )
    return (cash_flow_list)
(only 1 value will be returned. i need the values for my 200 rows of panda data frame)

i have a panda data frame which consist of 200 rows and 4 columns, the 4 columns store the values for the
fv, cp_rate, maturity_year and coupon_per_year.

how do i apply the user defined function and store the answers in a new column?
perhaps try something like :
df['cashflow'] = df.apply( cash_flow_list(df['fv'],df['cp_rate'],df['maturity_year'],df['coupon_per_year'])
** But I am new to python and pandas - usually doesnt do what I want/need
hi, thank you for your suggestion. i tried but it seems to be an error. i just started python a few backs so i am really beginnger.
File "<ipython-input-46-1029e367df7d>", line 1 bondtable['cashflow'] = bondtable.apply( bond_price(all_cash_flows(bondtable['face value'],bondtable['coupon rate'],bondtable['Maturity'],bondtable['N'])) ^ SyntaxError: unexpected EOF while parsing
When you are working with Pandas, it is important to think in vectorized form. Don't use loops, you can operate with entire dataframe columns. Express what you are trying to do. I don't know what "fv, cp_rate, maturity_year, coupon_per_year" variables are. Are they assumed to be columns of a dataframe? If so, you can likely rewrite your function in fully vectorized form.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 474 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,322 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Help to control pandas.melt function vanphuht91 0 810 Jun-17-2023, 08:41 AM
Last Post: vanphuht91
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,262 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,578 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,544 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,384 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,992 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,678 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,805 Aug-14-2021, 12:38 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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