【问题标题】:Replace certain value in pandas Dataframe without knowing neither column nor row在既不知道列也不知道行的情况下替换 pandas Dataframe 中的某些值
【发布时间】:2020-10-12 09:12:12
【问题描述】:

我想用 Python 替换我的 Pandas 数据框中的一个值。 (用字符串替换浮点数)。我知道值本身,但不知道列或行,然后想用不同的输入运行它。 我有以下数据框:

     P1899       P3486      P4074      P3352       P3500      P3447
Time                                                                
1997  100.0   89.745739  85.198939  87.377584  114.755270  81.131599
1998  100.0  101.597557  83.468442  86.369083  106.031629  95.263796
1999  100.0   97.234551  91.262551  88.759609  104.539337  95.859980
2000  100.0  100.759918  74.236098  88.295711  103.739557  90.272329
2001  100.0   96.873469  86.075067  87.530995  106.371072  91.807542
2002  100.0   95.000000  90.313561  82.699342  109.279845  94.444444

现在我想用“OVER”替换大于 110 的值,用“UNDER”替换小于 90 的值。 我使用了以下内容,因为我无法通过 for 循环获得任何结果。 我使用了 lambda:

annual_rainfall_perc = annual_rainfall_perc.apply(lambda x: np.where(x > 110, 2000, x))
annual_rainfall_perc = annual_rainfall_perc.apply(lambda x: np.where(x < 90, 'UNDER', round(x, 2)))

这里我用 2000 替换了所有更大的值,因为否则第二个 lambda 将无法检查包含浮点数和字符串的数据帧... 我的数据框现在如下所示:

     P1899   P3486  P4074  P3352   P3500  P3447
Time                                            
1997  100.0   Under  Under  Under  2000.0  Under
1998  100.0   101.6  Under  Under  106.03  95.26
1999  100.0   97.23  91.26  Under  104.54  95.86
2000  100.0  100.76  Under  Under  103.74  90.27
2001  100.0   96.87  Under  Under  106.37  91.81
2002  100.0    95.0  90.31  Under  109.28  94.44

所以现在我打算用“OVER”替换所有等于 2000 的值。我该怎么做?

我试过了:

for x in annual_rainfall_perc:
    for i in x:
        if i == 2000:
            annual_rainfall_perc[x][i]= 'Over'
        else:
            annual_rainfall_perc=annual_rainfall_perc
print(annual_rainfall_perc)

但数据框中没有任何变化。 还有其他方法吗?

【问题讨论】:

    标签: python pandas dataframe replace


    【解决方案1】:

    使用mask 非常简单:

    df.mask(df>110,'OVER').mask(df<90,'UNDER')
    

    结果:

          P1899    P3486    P4074  P3352    P3500    P3447
    Time                                                  
    1997    100    UNDER    UNDER  UNDER     OVER    UNDER
    1998    100  101.598    UNDER  UNDER  106.032  95.2638
    1999    100  97.2346  91.2626  UNDER  104.539    95.86
    2000    100   100.76    UNDER  UNDER   103.74  90.2723
    2001    100  96.8735    UNDER  UNDER  106.371  91.8075
    2002    100       95  90.3136  UNDER   109.28  94.4444
    

    【讨论】:

      【解决方案2】:

      这是一种以矢量化方式执行此操作的方法。在单独的数据框中进行所有字符串操作,然后一次性分配相关值:

      new_df = df.copy()
      
      new_df.loc[:, :] = " "
      new_df[df > 110] = "over"
      new_df[df < 90] = "under"
      
      df[(df < 90) | (df > 110)] = new_df
      

      结果:

            P1899    P3486    P4074  P3352    P3500    P3447
      Time                                                  
      1997  100.0    under    under  under     over    under
      1998  100.0  101.598    under  under  106.032  95.2638
      1999  100.0  97.2346  91.2626  under  104.539    95.86
      2000  100.0   100.76    under  under   103.74  90.2723
      2001  100.0  96.8735    under  under  106.371  91.8075
      2002  100.0       95  90.3136  under   109.28  94.4444
      

      【讨论】:

        【解决方案3】:

        这也有效:

        df = df.transform(lambda x: np.select([x.gt(110), x.lt(90)], ['Over', 'Under'], round(x, 2)))
        

        【讨论】:

          猜你喜欢
          • 2014-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-24
          相关资源
          最近更新 更多