测试Pandas lambda函数中的字典条目(Testing dictionary entries in a Pandas lambda function)

编程入门 行业动态 更新时间:2024-10-24 19:17:29
测试Pandas lambda函数中的字典条目(Testing dictionary entries in a Pandas lambda function)

当我做:

​df['ViewClass'] = df['dataset_id'].apply( lambda x: classdict[str(x)] if classdict[str(x)] else '???' )

如果钥匙不存在,这会爆炸。 我怎样才能得到lambda函数的行为,并把'???' 如果密钥不存在?

我来自Perl背景,并说明我的错误首先尝试在这里:)

When I do:

​df['ViewClass'] = df['dataset_id'].apply( lambda x: classdict[str(x)] if classdict[str(x)] else '???' )

This blows up if the key does not exist. How can I get get the lambda function to behave and put in '???' if the key does not exist?

I come from a Perl background and that accounts for my faulty first try here :)

最满意答案

看来你正在寻找Series.map:

df['ViewClass'] = df['dataset_id'].astype(str).map(classdict).fillna('???')

.astype(str)将列转换为对象。 然后, .map在字典中查找相应的值。 如果找不到密钥,则返回nan 。 因此,最后你会用指定的值填充nan s。

通常使用矢量化方法(如map而不是遍历Series或DataFrame(这是应用程序的做法)更好。

It seems you are looking for Series.map:

df['ViewClass'] = df['dataset_id'].astype(str).map(classdict).fillna('???')

.astype(str) converts the column to object. Then .map looks up the corresponding values in the dictionary. It returns nan if it cannot find the key. Therefore, at the end you'll fill nans with the specified value.

It is generally better to use vectorized methods like map instead of iterating over a Series or a DataFrame (that is what apply does).

更多推荐

本文发布于:2023-04-29 09:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   字典   函数   测试   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!