Reputation: 471
I want to check for the following dataframe whether multiple values are empty and then based on that condition assign a 0 to the document type column:
Document Type Totaal Factuurdatum
-- ----------------------------------- -------- --------------
0 0 nan nan
21 IngramMicro.AccountsPayable.Invoice 562.65 2021-03-16
I'm using the following working code for one column:
df.loc[df['Totaal'] == 'NaN', 'Document Type'] = 0
However, can not make it work to check if ONE column of a list of columns is nan and then assign a 0 .. So which results in more often assigning a 0.
Desired output:
Document Type Totaal Factuurdatum
-- ----------------------------------- -------- --------------
0 0 nan nan
21 0 562.65 nan
Upvotes: 0
Views: 121
Reputation: 863331
I think you need DataFrame.any
for test if at least one value matching by list of columns or DataFrame.all
for test if all values match:
cols = ['Totaal','Factuurdatum']
df.loc[(df[cols] == 'NaN').any(axis=1), 'Document Type'] = 0
#laternative for test NaN and nan strings
df.loc[(df[cols].isin(['NaN', 'nan']).any(axis=1), 'Document Type'] = 0
If NaNs are missing values:
cols = ['Totaal','Factuurdatum']
df.loc[df[cols].isna().any(axis=1), 'Document Type'] = 0
Upvotes: 1