[英]Function does not correctly recognize condition
My dataframe (dfd) has a column ('Verleihgruppe') in which numerous articles can be found that were rented. 我的数据框(dfd)有一列('Verleihgruppe'),可以在其中找到许多已租借的文章。 For all records with NaN, products were purchased.
对于使用NaN的所有记录,都购买了产品。 Therefore, I try to create a column ('Geschäftsvorgang1') for that assigns the term 'Rental' to all records of that have a value whereas 'Purchase' should be assigned for all NaN.
因此,我尝试为该字段创建一个列('Geschäftsvorgang1'),以为所有具有值的记录分配术语“出租”,而应为所有NaN分配“购买”。
The problem is that the newly created column only contains the strings 'Rental'. 问题在于新创建的列仅包含字符串“ Rental”。 Obviously, it did not recognize the NaN in order to assign these records the right string 'Purchase'.
显然,它无法识别NaN以便为这些记录分配正确的字符串“购买”。
It would be great if you could help! 如果您能提供帮助,那就太好了!
Thanks! 谢谢!
def product_type(x):
if x['Verleihgruppe'] == 'NaN':
return 'Purchase'
else:
return 'Rental'
dfd['Geschäftsvorgang1'] = dfd.apply(product_type, axis=1)
It's never good to let NaN
values in DataFrame so you should replace NaN
values by 0 thanks to fillna() function and make sure to have numeric data in the Verleihgruppe
column: 在DataFrame中让
NaN
值永远都不是一件好事,因此您应该使用fillna()函数将NaN
值替换为0,并确保在Verleihgruppe
列中包含数字数据:
import pandas as pd
dfd.fillna(value=0, inplace=True)
dfd['Geschäftsvorgang1'] = ["Purchase" for r in dfd['Verleihgruppe'] if r == 0 else "Rental"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.