Python123new
Python123new

Reputation: 31

Creating new columns based on sum of specific columns

I am very new to Python and trying to create a column to indicate whether the sum of few columns are greater than 0 or not.

For example, I have a df that is similar to the one below:

id quantity spent freq date
a1 0 0 0 10/02
a2 0 10 0 05/12
a2 0 0 0 03/12

If a row for 'quantity', 'spent', and 'freq' is all 0, I want to create a new column to indicate that the row has all 0 values like the table below:

id quantity spent freq date not_zero
a1 0 0 0 10/02 0
a2 0 10 0 05/12 1
a2 0 0 0 03/12 0

Upvotes: 0

Views: 40

Answers (2)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Firstly create a boolean mask:

mask=(df[['quantity','spent','freq']]==0).all(1)

Finally:

with loc accessor:

df.loc[mask,'not_zero']=0
df.loc[~mask,'not_zero']=1

OR

with np.where() method:

df['not_zero'] = np.where(mask, 0, 1)

Upvotes: 0

Henry Ecker
Henry Ecker

Reputation: 35626

Try any on axis=1 then astype:

df['not_zero'] = df[['quantity', 'spent', 'freq']].any(axis=1).astype(int)

Or with np.where:

df['not_zero'] = np.where(df[['quantity', 'spent', 'freq']].any(axis=1), 1, 0)

df:

   id  quantity  spent  freq   date  not_zero
0  a1         0      0     0  10/02         0
1  a2         0     10     0  05/12         1
2  a2         0      0     0  03/12         0

Upvotes: 1

Related Questions