zorny zorny - 1 year ago 167
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)

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

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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download