作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的数据框:
Nickname Vpip% Pfr% Agg Hands
0 -2dV2dA- 25.675676 16.666667 1.187500 222
1 06Hookd 26.470588 16.176471 1.000000 68
2 0spiel2632 23.794212 17.363344 0.750000 311
3 10Pet10 23.214286 7.142857 1.000000 56
4 12022015a 75.000000 NaN NaN 4
5 17bastian 31.034483 6.896552 1.000000 29
6 1990shawn 26.666667 6.666667 0.500000 15
7 199376966 66.666667 25.000000 1.000000 12
8 1MILKSHAKE 75.000000 50.000000 NaN 4
9 1nheritance 30.136986 20.547945 1.000000 73
10 2016deal 63.076923 12.307692 1.142857 65
11 26tj26 43.589744 30.769231 1.333333 39
12 2SMUG4YOU 70.000000 20.000000 1.000000 10
13 320t 71.074380 38.842975 1.410256 121
14 4cheeze126 52.205882 34.926471 0.900000 272
15 67bishop 19.148936 2.127660 1.000000 47
16 777aprilia 23.076923 15.384615 NaN 13
17 77gost77129 17.241379 14.942529 NaN 87
18 7r3m5 19.942197 16.473988 1.111111 346
19 888sharkbait 50.000000 30.769231 0.750000 26
我想再根据值制作一列。我正在使用 np.where 来设置值,但它只能根据条件设置 2 个值。我需要输入很多值。我尝试用 np.where 来做,但它互相替换。
Data['Badge'] = np.where((Data['Hands'] > 30) & (Data['Vpip%'] <= 18.0), 'Rock', np.nan)
Data['Badge'] = np.where((Data['Hands'] > 30) & (Data['Vpip%'] > 60.0), 'Whale', np.nan)
Data['Badge'] = np.where((Data['Hands'] > 30) & (Data['Vpip%'] <= 60.0) & ((Data['Vpip%'] > 40.0)), 'Fish', np.nan)
Data['Badge'] = np.where((Data['Hands'] > 30) & (Data['Vpip%'] <= 40.0) & ((Data['Vpip%'] > 27.0) & (Data['Agg'] < 2.0)), 'Loose Passive', np.nan)
Data['Badge'] = np.where((Data['Hands'] > 30) & (Data['Vpip%'] <= 40.0) & ((Data['Vpip%'] > 27.0) & (Data['Agg'] > 2.0)), 'Loose Aggresive', np.nan)
有什么想法吗?
最佳答案
对于多种条件,请使用 np.select
相反:
c1 = (Data['Hands'] > 30) & (Data['Vpip%'] <= 18.0)
c2 = (Data['Hands'] > 30) & (Data['Vpip%'] > 60.0)
c3 = (Data['Hands'] > 30) & (Data['Vpip%'] <= 60.0)
c4 = ((Data['Hands'] > 30) & (Data['Vpip%'] <= 40.0)
& ((Data['Vpip%'] > 27.0) & (Data['Agg'] < 2.0)))
c5 = ((Data['Hands'] > 30) & (Data['Vpip%'] <= 40.0)
& ((Data['Vpip%'] > 27.0) & (Data['Agg'] > 2.0)))
Data['Badge'] = (np.select(condlist = [c1,c2,c3,c4,c5],
choicelist = ['Rock', 'Whale', 'Fish', 'Loose Passive',
'Loose Aggresive'],
default = np.nan))
<小时/>
print(Data)
Nickname Vpip% Pfr% Agg Hands Badge
0 -2dV2dA- 25.675676 16.666667 1.187500 222 Fish
1 06Hookd 26.470588 16.176471 1.000000 68 Fish
2 0spiel2632 23.794212 17.363344 0.750000 311 Fish
3 10Pet10 23.214286 7.142857 1.000000 56 Fish
4 12022015a 75.000000 NaN NaN 4 nan
5 17bastian 31.034483 6.896552 1.000000 29 nan
6 1990shawn 26.666667 6.666667 0.500000 15 nan
7 199376966 66.666667 25.000000 1.000000 12 nan
8 1MILKSHAKE 75.000000 50.000000 NaN 4 nan
9 1nheritance 30.136986 20.547945 1.000000 73 Fish
10 2016deal 63.076923 12.307692 1.142857 65 Whale
11 26tj26 43.589744 30.769231 1.333333 39 Fish
12 2SMUG4YOU 70.000000 20.000000 1.000000 10 nan
13 320t 71.074380 38.842975 1.410256 121 Whale
14 4cheeze126 52.205882 34.926471 0.900000 272 Fish
15 67bishop 19.148936 2.127660 1.000000 47 Fish
16 777aprilia 23.076923 15.384615 NaN 13 nan
17 77gost77129 17.241379 14.942529 NaN 87 Rock
18 7r3m5 19.942197 16.473988 1.111111 346 Fish
19 888sharkbait 50.000000 30.769231 0.750000 26 nan
关于python - 设置超过2个值取决于条件,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56272917/
我是一名优秀的程序员,十分优秀!