How to Replace Values in Column Based on Condition in Pandas?

How to Replace Values in Column Based on Condition in Pandas?

In Pandas, you can replace values in a column based on a condition using the loc[] indexer or the where() method or even the replace() method. Here's how you can use each of these methods:

1. Using loc[]:

Let's say we have the following DataFrame, and we want to replace all values in the 'age' column that are below 18 with the value 0:

import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie', 'David'], 'age': [15, 25, 10, 30]} df = pd.DataFrame(data) df.loc[df['age'] < 18, 'age'] = 0 print(df) 

2. Using where():

You can use the where() method to achieve the same result:

df['age'] = df['age'].where(df['age'] >= 18, 0) print(df) 

With the where() method, the first argument is a condition, and the second argument is the value you want to replace the non-complying elements with.

3. Using replace():

The replace() method is more general and can be used to replace specific values. If you want to replace based on conditions, you can use it in combination with a condition:

df['age'] = df['age'].replace(df[df['age'] < 18]['age'], 0) print(df) 

Any of these methods will produce the following DataFrame:

 name age 0 Alice 0 1 Bob 25 2 Charlie 0 3 David 30 

Choose the method that you find the most intuitive and readable for your specific use case.


More Tags

talkback ts-node tabpage apex-code pyinstaller textarea relational-database node-webkit redis-py iot

More Programming Guides

Other Guides

More Programming Examples