分组依据列表中的值

时间:2019-01-04 21:36:31

标签: pandas

我有一个很大的数据集,我需要在其中基于称为AoIs的列进行计算。例如,对于Name=="P_01"的所有点,我希望front列在AoIs的所有持续时间之和。然后,我想对sideconcerns_form做同样的事情。我将df按名称分组,但是我尝试过的任何扭曲列表操作都失败了。

以下是我正在使用的数据的摘要:

      Name               AoIs  duration
0     P_01                NaN    1704.0
1     P_01                NaN    1654.0
2     P_01                NaN     731.0
3     P_01                NaN    3317.0
4     P_01                NaN     897.0
5     P_01                NaN     773.0
6     P_01                NaN    1155.0
7     P_01       [side,front]    1064.0
8     P_01    [concerns_form]     299.0
9     P_01    [concerns_form]     390.0

以下是创建类似于我正在使用的df的代码段:

df = pd.read_json('{"Name":{"0":"P_01","1":"P_01","2":"P_01","3":"P_01","4":"P_01","5":"P_01","6":"P_01","7":"P_01","8":"P_01","9":"P_01","10":"P_01","11":"P_01","12":"P_01","13":"P_01","14":"P_01","15":"P_01","16":"P_01","17":"P_01","18":"P_01","19":"P_01"},"AoIs":{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":["front", "side"],"8":["concerns_form","side"],"9":["concerns_form"],"10":["concerns_form"],"11":["concerns_title"],"12":["concerns_form"],"13":["concerns_submit"],"14":["side_nav"],"15":["concerns_title"],"16":["side_nav"],"17":["concerns_form"],"18":["concerns_title"],"19":["concerns_title"]},"duration":{"0":1704.0,"1":1654.0,"2":731.0,"3":3317.0,"4":897.0,"5":773.0,"6":1155.0,"7":1064.0,"8":299.0,"9":390.0,"10":1612.0,"11":1396.0,"12":2236.0,"13":798.0,"14":274.0,"15":182.0,"16":440.0,"17":166.0,"18":382.0,"19":282.0}}')

3 个答案:

答案 0 :(得分:2)

我会添加一些新列,然后进行一些分组。

df['side'] = df['AoIs'].map(str).str.contains('side')
df['front'] = df['AoIs'].map(str).str.contains('front')
df['concerns_form'] = df['AoIs'].map(str).str.contains('concerns_form')

然后例如:

df[df['side']==True].groupby('Name').sum()

答案 1 :(得分:0)

使用过滤后的groupby

target = 'front'
df[[target in x if isinstance(x,list) else False for x in df.AoIs]].groupby('Name').duration.sum()

输出:

Name
P_01    1064

答案 2 :(得分:0)

您可以将“ AoIs”列分为两列,然后可以按任一列或两列进行分组。这也使您可以更改分组条件

df [[''Right','Left']] = df ['AoIs']。str.split(',',expand = True)