Python 根据阈值字典替换某些列中的值?

Python 根据阈值字典替换某些列中的值?,python,pandas,Python,Pandas,我试图从这个熊猫df中得到: mag ip em as_ppm au_ppm 0 820 6447 99 4670 30 1 774 5827 26 35 97 2 800 9089 75 9727 25 3 584 6122 38 2911 80 4 494 7616 78 6673 67 5 742 6626 30 9424 69 6

我试图从这个熊猫df中得到:

   mag  ip    em   as_ppm  au_ppm
0  820  6447  99    4670      30
1  774  5827  26      35      97
2  800  9089  75    9727      25
3  584  6122  38    2911      80
4  494  7616  78    6673      67
5  742  6626  30    9424      69
6  803  2136  71    4043      73
7  682  8172  43    8806      26
8  132  1369  41    8267      34
9  680  5536  41    4431      16
使用这些阈值:

lowThresholds = {'mag':500,'ip':5000, 'em':0, 'as_ppm':0, 'au_ppm':0}
highThresholds = {'mag':1000,'ip':7500, 'em':90, 'as_ppm':8000, 'au_ppm':90}
对于具有相同形状的矩阵,使用True和False:

   mag   ip    em    as_ppm  au_ppm
0  True  True  False True    True 
1  True  True  True  True    False
2  True  False True  False   True 
3  True  True  True  True    True 
4  True  False True  True    True 
5  True  True  True  False   True 
6  True  False True  True    True 
7  True  False True  False   True 
8  False False True  False   True 
9  True  True  True  True    True 
最好使用:

weights = {'mag':5,'ip':10, 'em':5, 'as_ppm':20, 'au_ppm':30}
结果是:

   mag    ip  em  as_ppm  au_ppm
0  5      10  5   20      30
1  5      10  5   20      0
2  5      0   5   0       30
3  5      10  5   20      30
4  5      0   5   20      30
5  5      10  5   0       30
6  5      0   5   20      30
7  5      0   5   0       30
8  0      0   5   0       30
9  5      10  5   20      30
通过创建各种新的数据帧,我发现了一些很糟糕的方法,但我知道它会扩展得很厉害。

对于掩码链和 然后使用 ,但我认为@YOBEN_的答案应该更好:

m = df.gt(lowThresholds) & df.lt(highThresholds)

df = df.mask(m, pd.Series(weights), axis=1).where(m, 0)
print (df)
   mag  ip  em  as_ppm  au_ppm
0    5  10   0      20      30
1    5  10   5      20       0
2    5   0   5       0      30
3    5  10   5      20      30
4    0   0   5      20      30
5    5  10   5       0      30
6    5   0   5      20      30
7    5   0   5       0      30
8    0   0   5       0      30
9    5  10   5      20      30
对于输入['mag'、'ip'、'as_ppm'、'au_ppm'、'em']的键:
df[key]=列表(map(df[key])。值,λx:x>lowThresholds[key]和x
试试看

for key in ['mag', 'ip', 'as_ppm', 'au_ppm', 'em']:
   df[key] = list(map(df[key].values, lambda x: x > lowThresholds[key] and x < highThresholds[key]
s=(df.lt(highThresholds) & df.gt(lowThresholds)).mul(weights)
   mag  ip  em  as_ppm  au_ppm
0    5  10   0      20      30
1    5  10   5      20       0
2    5   0   5       0      30
3    5  10   5      20      30
4    0   0   5      20      30
5    5  10   5       0      30
6    5   0   5      20      30
7    5   0   5       0      30
8    0   0   5       0      30
9    5  10   5      20      30