简体   繁体   中英

Conditional formatting using GroupBy

I am working with movement data - basically users and their interactions with sensors at different locations. I need to define a user type based on various factors. Currently the easiest way to define one user type is with their interactions with various sensors.

In my head this should be quite simple. I need to group the users via the User column together and then search the Sensor column of my dataset for the specific sensors. Should one of the specific sensors be located, then the User type for the entire group should be defined accordingly.

It seems easy when I think of it but I am having difficulty wording it in Python. I am quite new to this, so any help would be appreciated!

Thusfar I have looked at creating the groups and trying to conditional format single rows.

This is a glimpse dataset which I am working with (I have set the User type to 0 for now):

         User  User type     Sensor        Date   Time in  Time out  \
0        User 0000043          0  Sensor 17  2019/04/29  08:33:00  08:34:00   
1        User 0000047          0  Sensor 05  2019/04/09  11:36:00  11:36:00   
2        User 0000047          0  Sensor 05  2019/04/09  12:31:00  12:31:00   
3        User 0000047          0  Sensor 05  2019/04/09  12:34:00  12:34:00   
4        User 0000047          0  Sensor 05  2019/04/09  13:47:00  13:47:00   
5        User 0000047          0  Sensor 05  2019/04/09  14:34:00  14:34:00   
6        User 0000047          0  Sensor 05  2019/04/09  15:03:00  15:04:00   
7        User 0000047          0  Sensor 05  2019/04/09  15:14:00  15:14:00   
8        User 0000047          0  Sensor 05  2019/04/09  15:24:00  15:26:00   
9        User 0000047          0  Sensor 05  2019/04/09  15:29:00  15:29:00

I have grouped the data via the following code:

rawsorted.groupby('User').Sensor.unique()

And this was my attempt to conditional format the rows:

rawsorted.loc[rawsorted['Sensor'] == 'Sensor 12|Sensor 13|Sensor 15|Sensor 16|Sensor 17|Sensor 23' , 'User type'] = 'NMT'

你正在寻找isin

rawsorted.loc[rawsorted['Sensor'].isin('Sensor 12|Sensor 13|Sensor 15|Sensor 16|Sensor 17|Sensor 23'.split('|') , 'User type'] = 'NMT'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM