Skip to main content
Post Closed as "Duplicate" by Trenton McKinney pandas
Question Protected by Trenton McKinney
edited title
Link
Zeugma
  • 32.3k
  • 9
  • 73
  • 85

How to apply custom function to pandas data frame for each row

Source Link
zorny
  • 695
  • 1
  • 8
  • 13

How to apply custom function to pandas data frame

I want to apply a custom function and create a derived column called population2050 that is based on two columns already present in my data frame.

import pandas as pd
import sqlite3
conn = sqlite3.connect('factbook.db')
query = "select * from facts where area_land =0;"
facts = pd.read_sql_query(query,conn)
print(list(facts.columns.values))

def final_pop(initial_pop,growth_rate):
    final = initial_pop*math.e**(growth_rate*35)
    return(final)

facts['pop2050'] = facts['population','population_growth'].apply(final_pop,axis=1)

When I run the above code, I get an error. Am I not using the 'apply' function correctly?