根据条件设置两个以上的值,python

时间:2019-05-23 10:09:18

标签: python pandas numpy

我有一个像这样的数据框:

        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)

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

对于多种情况,请改用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