Home > Backend Development > Python Tutorial > How to Replace Values in a DataFrame Column Based on a Condition?

How to Replace Values in a DataFrame Column Based on a Condition?

Mary-Kate Olsen
Release: 2024-10-30 23:51:30
Original
1084 people have browsed it

How to Replace Values in a DataFrame Column Based on a Condition?

Replacing Values in a DataFrame Column Based on Condition

In this scenario, you have a DataFrame with a column named 'First Season'. You wish to modify the values in this column, specifically replacing those higher than 1990 with the value 1. While the attempt made is to select values using df.loc[(df['First Season'] > 1990)] = 1, it replaces all values in the selected rows instead of targeting only the 'First Season' column.

To effectively replace values within a specific column based on a condition, it is essential to clearly specify the column in the selection criteria. This is achieved using the following syntax:

<code class="python">df.loc[<mask>, <optional column(s)>]</code>
Copy after login

In your case, the mask is used to select rows with 'First Season' values greater than 1990. The code below will accomplish the desired outcome:

<code class="python">df.loc[df['First Season'] > 1990, 'First Season'] = 1</code>
Copy after login

Alternatively, if you intend to create a boolean indicator, you can utilize the boolean condition to generate a boolean Series and convert True and False values to integers using .astype(). This approach will generate a column with 1 (True) and 0 (False) values.

<code class="python">df['First Season'] = (df['First Season'] > 1990).astype(int)</code>
Copy after login

The above is the detailed content of How to Replace Values in a DataFrame Column Based on a Condition?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template