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

How to Replace Specific Values in a Pandas DataFrame Column Based on Conditions?

Mary-Kate Olsen
Release: 2024-11-02 20:41:02
Original
206 people have browsed it

How to Replace Specific Values in a Pandas DataFrame Column Based on Conditions?

Pandas DataFrame: Targeted Value Replacement Based on Conditions

In Pandas, it's often necessary to modify specific values within a DataFrame based on certain criteria. While a common approach is to use loc to select rows, it's crucial to understand how to precisely target a specific column for value modification.

Consider the following DataFrame, where we wish to replace values in the 'First Season' column that exceed 1990 with the integer 1:

                 Team  First Season  Total Games
0      Dallas Cowboys          1960          894
1       Chicago Bears          1920         1357
2   Green Bay Packers          1921         1339
3      Miami Dolphins          1966          792
4    Baltimore Ravens          1996          326
5  San Franciso 49ers          1950         1003
Copy after login

An initial attempt using only the loc function resulted in replacing all values in the selected rows rather than solely the targeted column. To rectify this, we need to explicitly specify the 'First Season' column as the second argument to loc:

df.loc[df['First Season'] > 1990, 'First Season'] = 1
Copy after login

This targeted approach ensures that only the values in the 'First Season' column satisfy the condition are replaced with 1, leaving the other columns unaffected.

                 Team  First Season  Total Games
0      Dallas Cowboys          1960          894
1       Chicago Bears          1920         1357
2   Green Bay Packers          1921         1339
3      Miami Dolphins          1966          792
4    Baltimore Ravens             1          326
5  San Franciso 49ers          1950         1003
Copy after login

Alternatively, if the desired outcome is a boolean indicator, you can employ the condition to create a boolean Series and convert it to integers, where True and False translate to 1 and 0, respectively:

df['First Season'] = (df['First Season'] > 1990).astype(int)
Copy after login

This approach yields a DataFrame with updated values:

                 Team  First Season  Total Games
0      Dallas Cowboys             0          894
1       Chicago Bears             0         1357
2   Green Bay Packers             0         1339
3      Miami Dolphins             0          792
4    Baltimore Ravens             1          326
5  San Franciso 49ers             0         1003
Copy after login

The above is the detailed content of How to Replace Specific Values in a Pandas DataFrame Column Based on Conditions?. 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