Home > Article > Backend Development > 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>
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>
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>
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!