我有以下资料:

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

最佳答案

使用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)应该有效

10-07 22:32