1

I am using pandas in python.

How can I set all the values of a DataFrame below 21000 by 'EstimatedSalary ' to zero? Means that I want the first two rows to have zeros instead of 19000 and 20000

ID          Gender  Age EstimatedSalary Purchased
15624510    Male    19  19000           0
15810944    Male    35  20000           0
15668575    Female  26  43000           0
15603246    Female  27  57000           0

2 Answers 2

1

Use:

df.loc[df['EstimatedSalary']< 21000, 'EstimatedSalary'] = 0

Or:

df['EstimatedSalary'] = df['EstimatedSalary'].mask(df['EstimatedSalary'] < 21000, 0)

Or:

df['EstimatedSalary'] = np.where(df['EstimatedSalary'] < 21000, 0, df['EstimatedSalary'])

print (df)
          D  Gender  Age  EstimatedSalary  Purchased
0  15624510    Male   19                0          0
1  15810944    Male   35                0          0
2  15668575  Female   26            43000          0
3  15603246  Female   27            57000          0
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! It works! I'll be able to mark the question as accepted in few minutes only)
@Fabi - Thank you.
1

This is one way:

df.loc[df['EstimatedSalary'] < 21000, 'EstimatedSalary'] = 0

Another way which utilizes the fact that bool is a subclass of int:

df['EsimatedSalary'] *= df['EstimatedSalary'] >= 21000

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.