基于Python中多个条件返回列的方法

基于Python中多个条件返回列的方法,第1张

基于Python中多个条件返回列的方法

要回答问题的第一部分,以下是您如何转录if语句的方法:

wifi_user_equality = (df.Wifi_User1 == df.Wifi_User2) |       (df.Wifi_User2 == df.Wifi_User3) |       (df.Wifi_User3 == df.Wifi_User1)thermostat_change = df.Thermostat != df.Thermostat.shift(1)

然后返回所有都为真的所有行:

df[wifi_user_equality & thermostat_change]         Wifi_User1  Wifi_User2  Wifi_User3  Thermostat  Act_User1  Act_User2   Act_User3 9-70         -70         -65          24          0        0.0          0.0

或者,如果您只想要这些的索引:

df.index[(wifi_user_equality & thermostat_change)]

对于问题的第二部分,这比较棘手,但这是一个解决方案:

# We add the first index element toozero = df.index == df.index[0]# Get the list of index where the condition is satisfied, in reverse orderidx = list(df.index[(wifi_user_equality & thermostat_change) | zero][::-1])for i, index in enumerate(idx):    if index > 0:        # I use a try/except block in case it cannot find an occurrence of 1        # (all previous act users are 0).        # Might not be needed in your specific application        try: x= df.loc[idx[i+1]:(index-1), ['Act_User1','Act_User2','Act_User3']] col_of_first_1 = np.where(x==1)[1][-1] + 1        except: col_of_first_1 = 'Not Found'        # Assign to a new column        df.loc[index, 'Last_Act_User'] = col_of_first_1

实际上:

我已经修改了您的数据,以使情况更复杂:

Wifi_User1      Wifi_User2      Wifi_User3      Thermostat   Act_User1   Act_User2  Act_User3-70  -70  -65  24   0    00-77  -48  -65  24   0    00-58  -48  -48  18   0    10-60  -56  -75  18   0    11-45  -60  -45  18   0    11-67  -45  -60  18   1    01-40  -65  -65  18   1    01-55  -78  -74  18   1    00-55  -45  -65  18   1    00-67  -45  -44  18   0    00-65  -68  -70  18   0    00-70  -70  -65  24   0    00-72  -56  -45  24   0    10-75  -45  -60  24   0    10-77  -48  -65  24   0    00

将给

df

    Wifi_User1  Wifi_User2  Wifi_User3  Thermostat  Act_User1  Act_User2            -70         -70         -65          24          0          0   1          -77         -48         -65          24          0          0   2          -58         -48         -48          18          0          1   3          -60         -56         -75          18          0          1   4          -45         -60         -45          18          0          1   5          -67         -45         -60          18          1          0   6          -40         -65         -65          18          1          0   7          -55         -78         -74          18          1          0   8          -55         -45         -65          18          1          0   9          -67         -45         -44          18          0          0   10         -65         -68         -70          18          0          0   11         -70         -70         -65          24          0          0   12         -72         -56         -45          24          0          1   13         -75         -45         -60          24          0          1   14         -77         -48         -65          24          0          0    Act_User3 Last_Act_User  00NaN  10NaN  20     Not Found  31NaN  41NaN  51NaN  61NaN  70NaN  80NaN  90NaN  10          0NaN  11          0  1  12          0NaN  13          0NaN  14          0NaN


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5667600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存