dimanche 4 décembre 2016

Return column based on multiple condition in Python

I have a DataFrame as below. Based on few conditions I need to retrieve the column

Wifi_User1      Wifi_User2      Wifi_User3    Thermostat    Thermo_Pos     Act_User1      Act_User2    Act_User3    Be_1   Be_2   Be_3
 -58             -48             -60             18             -50             0               1           0        1       1      3
 -67             -45             -60             18             -50             1               0           0        1       2      3
 -40             -65             -65             18             -50             1               0           1        3       2      3
 -67             -45             -44             18             -50             0               0           1        2       3      4
 -65             -68             -70             18             -50             0               0           0        1       3      2
 -55             -60             -65             24             -50             0               0           0        1       3      2
 -72             -56             -45             24             -50             0               1           0        2       1      1
 -75             -45             -60             24             -50             1               1           0        3       1      1
 -77             -48             -65             24             -50             1               1           0        1       1      2
 -70             -58             -68             24             -50             1               0           0        1       2      3
 -70             -75             -65             24             -50             0               0           0        1       2      3
 -55             -45             -65             24             -50             0               0           0        1       3      4
 -55             -55             -55             15             -50             0               0           0        1       1      4
 -65             -68             -70             15             -50             0               1           0        2       1      1
 -55             -45             -65             15             -50             0               1           0        1       1      4
 -67             -45             -44             15             -50             1               0           0        2       1      4
 -65             -68             -70             15             -50             1               0           0        2       1      1
 -67             -45             -44             15             -50             1               0           0        2       1      4
 -65             -68             -70             15             -50             1               0           0        2       1      1
 -55             -55             -55             10             -50             0               0           0        2       1      4
 -67             -45             -44             10             -50             0               0           0        2       3      4
 -65             -68             -70             10             -50             0               0           0        2       3      1

I have two conditions based on which I need to return the appropriate column

1st Condition:

 if (Wifi_User1!=Wifi_User2) or (Wifi_User2!=Wifi_User3)
 or (Wifi_User3!=Wifi_User1) or (Wifi_User1!=Wifi_User2!=Wifi_User3) 
 and when the thermostat value is changing

Then take, the difference between Wifi_user1 & Thermo_Pos, and same goes for 2 and 3. Taake the minimum among these difference, and return 1 in if its Wifi_User1, 2 if its Wifi_User2 and 3 if its Wifi_User3 in the new column say, "User".

2nd Condition:

 if (Wifi_User1==Wifi_User2) or (Wifi_User2==Wifi_User3)
 or (Wifi_User3==Wifi_User1) or (Wifi_User1==Wifi_User2==Wifi_User3) 
 and when the thermostat value is changing

then

scan Act_User1, Act_User2, Act_User3 columns for the first instance of 1 before the thermostat value changes and check the corresponding Beacon value( Be_1, Be_2, Be_3)

  If the Beacon value is "1"
  and 
  If its Act_user1, return 1 
  else if its Act_User2 return 2
  else return 3

From the above Dataframe,

At 5th row, the wifi_users are different and the thermostat value is changing from 18 to 24. Hence, 1st condition satisfies, and should return the minimum difference.

At 12th row, the wifi_users are same and the thermostat value is changing from 24 to 15. Hence, 2nd condition satisfies. And the first instance of 1 is Act_User1 and the corresponding Be_1 is 1, hence return 1

At 19th row, the wifi_users are same and the thermostat value is changing from 15 to 10. Here, the first instance of 1 is Act_User1 , but the corresponding Be_1 is not 1. Hence the condition is not satisfies. The next instance if 1 is at row 14th, where Act_User2 is 1 and corresponding Be_2 is 1, hence return 2.

Please let me know, how to go about it. I have tried few codes and tried intergrating altogether, but its not giving the required results

Aucun commentaire:

Enregistrer un commentaire