我有以下内容:
result.head(4)
district end party start state type id.thomas current
564 1 1987 Democrat 1985-01-03 HI rep 2 1985
565 1 1993 Democrat 1991-01-03 HI rep 2 1991
566 1 1995 Democrat 1993-01-05 HI rep 2 2019
567 1 1997 Democrat 1995-01-04 HI rep 2 2017
我想将end
列中的所有大于2014的值更改为2014
。我不知道该怎么做呢
答案 0 :(得分:4)
使用clip_upper
:
In [207]:
df['end'] = df['end'].clip_upper(1990)
df
Out[207]:
district end party start state type id.thomas current
564 1 1987 Democrat 1985-01-03 HI rep 2 1985
565 1 1990 Democrat 1991-01-03 HI rep 2 1991
566 1 1990 Democrat 1993-01-05 HI rep 2 2019
567 1 1990 Democrat 1995-01-04 HI rep 2 2017
因此,在您的情况下df['end'] = df['end'].clip_upper(2014)
应该正常工作