如何将自定义函数应用于每行的 pandas 数据框 [英] How to apply custom function to pandas data frame for each row

查看:56
本文介绍了如何将自定义函数应用于每行的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用一个自定义函数并创建一个名为population2050 的派生列,该列基于我的数据框中已存在的两列.

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?

推荐答案

Apply 将沿轴为 1 的整行传递给您.假设你的两列被称为 initial_popgrowth_rate

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)

这篇关于如何将自定义函数应用于每行的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆