在不知道列和行的情况下替换 Pandas Dataframe 中的某些值

ma2*_*020 6 python replace dataframe pandas

我想用 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
Run Code Online (Sandbox Code Playgroud)

现在我想用“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)))
Run Code Online (Sandbox Code Playgroud)

在这里,我用 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
Run Code Online (Sandbox Code Playgroud)

所以现在我打算用“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)
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*tef 3

使用非常简单mask

df.mask(df>110,'OVER').mask(df<90,'UNDER')
Run Code Online (Sandbox Code Playgroud)

结果:

      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
Run Code Online (Sandbox Code Playgroud)