0% found this document useful (0 votes)
476 views

Python - Pandas DataFrame - Replace All Values in A Column, Based On Condition - Stack Overflow

The document discusses replacing values in a Pandas DataFrame column based on a condition. It describes how using df.loc to select rows and replace the entire row, not just the desired column. The answer shows that to replace just the column values, you need to select the column after the condition in df.loc, like df.loc[condition, 'column'] = value. It also provides an alternative method of creating a boolean mask and casting it to integers to replace values with 1 and 0.

Uploaded by

funkrocknow3826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
476 views

Python - Pandas DataFrame - Replace All Values in A Column, Based On Condition - Stack Overflow

The document discusses replacing values in a Pandas DataFrame column based on a condition. It describes how using df.loc to select rows and replace the entire row, not just the desired column. The answer shows that to replace just the column values, you need to select the column after the condition in df.loc, like df.loc[condition, 'column'] = value. It also provides an alternative method of creating a boolean mask and casting it to integers to replace values with 1 and 0.

Uploaded by

funkrocknow3826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

02/03/2018 python - Pandas DataFrame: replace all values in a column, based on condition - Stack Overflow

Pandas DataFrame: replace all values in a column, based on condition

I have a simple DataFrame like the following:

I want to select all values from the 'First Season' column and replace those that are over 1990 by 1. In this example, only Baltimore Ravens would have the
1996 replaced by 1 (keeping the rest of the data intact).

I have used the following:

df.loc[(df['First Season'] > 1990)] = 1

But, it replaces all the values in that row by 1, and not just the values in the 'First Season' column.

How can I replace just the values from that column?

python python-2.7 pandas dataframe

asked Jul 20 '15 at 8:35


ichimok
177 1 2 9

1 Answer

Não encontrou uma resposta? Pergunte em Stack Overflow em Português. ✕

You need to select that column:

In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df

Out[41]:
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

So the syntax here is:

df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]

You can check the docs and also the 10 minutes to pandas which shows the semantics

EDIT

If you want to generate a boolean indicator then you can just use the boolean condition to generate a
boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:

In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df

Out[43]:
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

https://stackoverflow.com/questions/31511997/pandas-dataframe-replace-all-values-in-a-column-based-on-condition 1/2
02/03/2018 python - Pandas DataFrame: replace all values in a column, based on condition - Stack Overflow

edited Jul 20 '15 at 8:51 answered Jul 20 '15 at 8:37


EdChum
133k 22 242 220

Worked great! Isn't it possible to impose multiple transformations? df.loc[df['First Season'] > 1990,
'First Season'] = 1 df.loc[df['First Season'] < 1990, 'First Season'] = 0 makes all the values in
that column return 0 – ichimok Jul 20 '15 at 8:46

You can use a trick df['First Season'] = (df['First Season'] > 1990).astype(int) this creates a
boolean Series and casting to int converts True and False to 1 and 0 respectively – EdChum Jul 20 '15
at 8:49

astype(int) is more Pandorable ! Good answer. – mythicalcoder Nov 1 '16 at 10:52

Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH Google Facebook

https://stackoverflow.com/questions/31511997/pandas-dataframe-replace-all-values-in-a-column-based-on-condition 2/2

You might also like