athiq
athiq

Reputation: 11

how to sum the columns in a dataframe if one column is passed as none in python

Summation of 2 columns (brand and country) to get the KPi values
data['kpi'][(data.brand=='ABC') & (data.country is None)].sum() This gives me 0

I was expecting the above line will be equal to the below and which in turn gives the answer

data['kpi'][(data.brand=='ABC')].sum()

Upvotes: 0

Views: 28

Answers (1)

Armali
Armali

Reputation: 19395

The expression data.country is None yields only a single bool False value, not the needed logical vector. One could expect that the expression data.country == None works, but for some (unknown to me) reason, it doesn't with pandas.DataFrame columns (it yields a vector of all False values, regardless of None items).
What I found to work is the expression data.country.isnull(), i. e.:

data['kpi'][(data.brand=='ABC') & data.country.isnull()].sum()

Upvotes: 1

Related Questions