1

I have a dataframe:

   1     0      1         2
   2     2      3         1
   3     8      2         9 

Expected output:

       1     0      1         0
       2     2      3         0
       3     8      2         9 
    

My initial thought process for this is

     df = np.where(df['Reported'] >= df['Goal'], df['Score'] = 0, df['Score'] 

This isn't working as needed.

tj judge
  • 626
  • 3
  • 17

2 Answers2

2

You are close, just replace df['Score'] = 0 with 0 for passed condition and assign the output to df.Score col.

df.Score = np.where(df['Reported'] >= df['Goal'],0, df['Score'] )

Output

    ID  Goal    Reported    Score
0   1   0       1           0
1   2   2       3           0
2   3   8       2           9
Utsav
  • 5,572
  • 2
  • 29
  • 43
1

You can use loc method for indexing :

df.loc[df['Reported'] >= df['Goal'],'Score'] = 0