繁体   English   中英

熊猫数据框在创建多个列的列上应用功能

[英]pandas dataframe apply function over column creating multiple columns

我在下面有pandas df,有几列,其中之一是ip_addresses

    df.head()
           my_id   someother_id  created_at        ip_address     state
308074     309115   2859690   2014-09-26 22:55:20   67.000.000.000  rejected
308757     309798   2859690   2014-09-30 04:16:56   173.000.000.000  approved
309576     310619   2859690   2014-10-02 20:13:12   173.000.000.000  approved
310347     311390   2859690   2014-10-05 04:16:01   173.000.000.000 approved
311784     312827   2859690   2014-10-10 06:38:39   69.000.000.000  approved

对于每个ip_address,我尝试返回description, city, country

我在下面编写了一个函数并尝试应用它

from ipwhois import IPWhois


def returnIP(ip) :
    obj = IPWhois(str(ip))
    result = obj.lookup_whois()

    description = result["nets"][len(result["nets"]) - 1 ]["description"]
    city = result["nets"][len(result["nets"]) - 1 ]["city"]
    country = result["nets"][len(result["nets"]) - 1 ]["country"]

    return [description, city, country]

# --- 

suspect['ipwhois'] = suspect['ip_address'].apply(returnIP)

我的问题是,这将返回一个列表,我需要三个单独的列。

任何帮助是极大的赞赏。 我是Pandas / Python的新手,所以如果有更好的方法来编写函数并使用Pandas,将非常有帮助。

from ipwhois import IPWhois

def returnIP(ip) :
    obj = IPWhois(str(ip))
    result = obj.lookup_whois()

    description = result["nets"][len(result["nets"]) - 1 ]["description"]
    city = result["nets"][len(result["nets"]) - 1 ]["city"]
    country = result["nets"][len(result["nets"]) - 1 ]["country"]

    return (description, city, country)


suspect['description'], suspect['city'], suspect['country'] = \
suspect['ip_address'].apply(returnIP)

我能够用另一个stackoverflow解决方案解决它

for n,col in enumerate(cols):
    suspect[col] = suspect['ipwhois'].apply(lambda ipwhois: ipwhois[n])

如果有更优雅的解决方法,请分享!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM