zorny zorny - 1 month ago 17
Python Question

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

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?

Answer

Apply will pass you along the entire row with axis=1. Adjust like this assuming your two columns are called initial_popand growth_rate

def final_pop(row):
    return row.initial_pop*math.e**(row.growth_rate*35)
Comments