小编ma2*_*020的帖子

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

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

python replace dataframe pandas

6
推荐指数
1
解决办法
158
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1

replace ×1