简体   繁体   English

当值超过阈值时,仅将某些列值设置为零

[英]Set only certain column values to zero when value exceeds threshold

I have a dataframe that looks like this:我有一个看起来像这样的数据框:

iso3    prod_level  alloc_key   cell5m  x   y   rec_type    tech_type   unit    whea_a  ... acof_pct_prod   rcof_pct_prod   coco_pct_prod   teas_pct_prod   toba_pct_prod   bana_pct_prod   trof_pct_prod   temf_pct_prod   vege_pct_prod   rest_pct_prod
35110   IND IN16011 9243059 3990418 74.875000   13.041667   P   A   mt  0.0 ... 1.0 1.0 1.0 1.0 1.0 0.958586    0.449218    1.0 1.0 0.004520
35109   IND IN16011 9243058 3990417 74.791667   13.041667   P   A   mt  0.0 ... 1.0 1.0 1.0 1.0 1.0 0.970957    0.459725    1.0 1.0 0.009037
35406   IND IN16003 9283093 4007732 77.708333   12.708333   P   A   mt  0.0 ... 1.0 1.0 1.0 1.0 1.0 0.883868    1.000000    1.0 1.0 0.012084
35311   IND IN16011 9273062 4003381 75.125000   12.791667   P   A   mt  0.0 ... 1.0 1.0 1.0 1.0 1.0 0.942550    0.381430    1.0 1.0 0.015024
35308   IND IN16011 9273059 4003378 74.875000   12.791667   P   A   mt  0.0 ... 1.0 1.0 1.0 1.0 1.0 0.991871    0.887494    1.0 1.0 0.017878

I want to set all values that are greater than 0.9 in columns that end in 'prod' to zero.我想将所有以“prod”结尾的列中大于 0.9 的值设置为零。 I can select only those columns like this:我只能选择这样的列:

cols2=[col for col in df.columns if col.endswith('_prod')]
df[cols2]
whea_pct_prod   rice_pct_prod   maiz_pct_prod   barl_pct_prod   pmil_pct_prod   smil_pct_prod   sorg_pct_prod   pota_pct_prod   swpo_pct_prod   cass_pct_prod   ... acof_pct_prod   rcof_pct_prod   coco_pct_prod   teas_pct_prod   toba_pct_prod   bana_pct_prod   trof_pct_prod   temf_pct_prod   vege_pct_prod   rest_pct_prod
35110   1.0 0.958721    0.359063    1.0 1.0 1.000000    1.0 1.0 1.00000 0.992816    ... 1.0 1.0 1.0 1.0 1.0 0.958586    0.449218    1.0 1.0 0.004520
35109   1.0 0.878148    0.200283    1.0 1.0 1.000000    1.0 1.0 1.00000 0.993140    ... 1.0 1.0 1.0 1.0 1.0 0.970957    0.459725    1.0 1.0 0.009037
35406   1.0 0.996354    0.980844    1.0 1.0 0.274348    1.0 1.0 0.99945 1.000000    ... 1.0 1.0 1.0 1.0 1.0 0.883318    1.000000    1.0 1.0 0.012084
35311   1.0 0.570999    0.341217    1.0 1.0 1.000000    1.0 1.0 1.00000 0.997081    ... 1.0 1.0 1.0 1.0 1.0 0.942550    0.381430    1.0 1.0 0.015024
35308   1.0 0.657520    0.161771    1.0 1.0 1.000000    1.0 1.0 1.00000 0.991491    ... 1.0 1.0 1.0 1.0 1.0 0.991871    0.887494    1.0 1.0 0.017878

Now, when I try and set the values greater than 0.9 to be zero, it does not work.现在,当我尝试将大于 0.9 的值设置为零时,它不起作用。

df[cols2][df[cols2]>0.9]=0

What should I be doing instead?我应该怎么做?

您可以使用df.where(cond, other)将值替换为other where cond == False

df[cols2] = df[cols2].where(df[cols]<=0.9, other=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 创建当长度超过特定值时重置的列计数器 - Pandas create column counter that resets when the length exceeds a certain value 仅选择低于特定阈值的值 - Pick values only below a certain threshold 仅当列在某些日期具有非零值但在其他日期具有零值时如何创建虚拟对象 - How to create a dummy only if a column has non-zero values for certain dates but zero for other dates 如何基于Python中另一列的值将某个列的值确定为零 - how to make a certain values of a column value to zero based on another column' value in Python 达到特定阈值后,在df列中获取部分值的总和 - get partial sum of values in df column once they reach a certain threshold 将 dataframe 列中低于某个阈值的值替换为 NaN - Replace values in a dataframe column that are below a certain threshold with NaN 将低于某个 CV2 颜色图阈值的值设置为透明 - Set the values below a certain threshold of a CV2 Colormap to transparent 在一列中设置整数值,在另一列中设置阈值整数? - Set integer value in a column for threshold integer in another column? 尝试删除重复项时,熊猫只删除某些列值 - Pandas drop only certain column values when trying to remove duplicates 当值超过某个阈值时,np.argsort 无法正确排序 - np.argsort not sorting correctly when value over a certain threshold
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM