当我做:
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).
更多推荐
发布评论