Reputation: 13
My dataframe (dfd) has a column ('Verleihgruppe') in which numerous articles can be found that were rented. For all records with NaN, products were purchased. 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.
The problem is that the newly created column only contains the strings 'Rental'. Obviously, it did not recognize the NaN in order to assign these records the right string 'Purchase'.
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)
Upvotes: 0
Views: 28
Reputation: 200
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:
import pandas as pd
dfd.fillna(value=0, inplace=True)
dfd['Geschäftsvorgang1'] = ["Purchase" for r in dfd['Verleihgruppe'] if r == 0 else "Rental"]
Upvotes: 1