![]() |
VOOZH | about |
Let's explore different methods to replace values in a Pandas DataFrame column based on conditions.
The dataframe.loc[] function allows us to access a subset of rows or columns based on specific conditions, and we can replace values in those subsets.
df.loc[df['column_name'] == 'some_value', 'column_name'] = 'new_value'Consider a dataset with columns 'name', 'gender', 'math score', and 'test preparation'. In this example, we will replace all occurrences of 'male' with 1 in the gender column.
Output:
Name gender math score test preparation
0 John 1 50 none
1 Jay 1 100 completed
2 sachin 1 70 none
3 Geetha female 80 completed
4 Amutha female 75 completed
5 ganesh 1 40 none
We can replace values in Column based on Condition in Pandas using the following methods:
The np.where() function from the NumPy library is another powerful tool for conditionally replacing values.
df['column_name'] = np.where(df['column_name'] == 'some_value', 'value_if_true', 'value_if_false')Here, we will replace 'female' with 0 and 'male' with 1 in the gender column.
Output:
Name gender math score test preparation
0 John 1 50 none
1 Jay 1 100 completed
2 sachin 1 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh 1 40 none
Pandas' mask() function can be used to replace values where a condition is met.
df['column_name'].mask(df['column_name'] == 'some_value', 'new_value', inplace=True)In this example, we replace 'female' with 0 in the gender column using the mask() function.
Output:
Name gender math score test preparation
0 John male 50 none
1 Jay male 100 completed
2 sachin male 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh male 40 none
The apply() function in combination with a lambda function is a flexible method for applying conditional replacements based on more complex logic.
Here, we will replace 'female' with 0 in the gender column using the apply() function and lambda.
Output:
Name gender math score test preparation
0 John male 50 none
1 Jay male 100 completed
2 sachin male 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh male 40 none
In this article, we’ve explored four effective methods to replace values in a Pandas DataFrame column based on conditions: using loc[], np.where(), masking, and apply() with a lambda function.