Pandas DataFrame:根据条件替换列中的所有值 [英] Pandas DataFrame: replace all values in a column, based on condition
问题描述
我有一个简单的DataFrame,如下所示:
I have a simple DataFrame like the following:
我想从第一季"列中选择所有值,并将1990年以后的值替换为1.在此示例中,只有巴尔的摩乌鸦将1996年替换为1(其余数据保持不变).
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).
我使用了以下内容:
df.loc[(df['First Season'] > 1990)] = 1
但是,它将行中的所有值替换为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?
推荐答案
您需要选择该列:
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
所以这里的语法是:
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
编辑
如果要生成布尔指示符,则可以使用布尔条件生成布尔系列并将dtype强制转换为int
,这会将True
和False
转换为1
和0
分别:
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
这篇关于Pandas DataFrame:根据条件替换列中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!