我想用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)
但数据框中的任何内容都没有更改。还有另一种方法吗?
使用非常简单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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句