![](/img/trans.png)
[英]Build new column from a function in a pandas Dataframe using values from DataFrame
[英]Using a column from a dataframe in a new function, and then attaching the returned value from that function to the dataframe
我目前有一个名为 df 的数据框,有 6 列。
['title' 'company' 'location' 'summary' 'days' 'Date']
我有一个名为Geocode(Location)的 function,它将位置作为输入并返回一个邮政编码。
我需要弄清楚如何在 df dataframe 的每个位置上调用地理编码(位置)function,然后将新的邮政编码 Z78E6221F6393D1356681DB398F14CZZ6D 中的新列插入我想要的 output 是 dataframe,带有标题、位置、日期、发布日期、邮政编码。
您可以使用transform
根据location
获取新列。
df['zipcode'] = df['location'].transform(geocode)
假设geocode
是在location
上运行的 function 。
这可以通过.apply()
来实现:
df.loc[:, 'ZipCode'] = df['Location'].apply(Geocode)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.