python-熊猫:更改列值超过一定数量

我有以下内容

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

我想将2014年底的所有大于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)应该可以工作

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...