熊猫:更改一定数量的列值

时间:2016-04-14 13:39:05

标签: python pandas

我有以下内容:

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。我不知道该怎么做呢

1 个答案:

答案 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)应该正常工作