Mahamutha M
Mahamutha M

Reputation: 1287

pandas groupby as list returns empty with both agg and apply function

When trying to return the "rssi" column as list based on the group of the columns i.e.,"master_mac" and "slave_mac", the pandas data frame returns empty and below is my input dataframe

   master_mac    slave_mac        uuid           rawData   rssi            
0  ac233fc01403  ac233f26492b     e2c56db5       NaN       -23                                                  
1  ac233fc01403  ac233f26492b     e2c56db5       NaN       -28                                                  
2  ac233fc01403  ac233f26492b     e2c56db5       NaN       -32                                                   
3  ac233fc01403  ac233f26492b     e2c56db5       NaN       -37
4  ac233fc01403  e464eecba5eb     NaN            590080    -25         
5  ac233fc01403  ac233f26492b     e2c56db5       NaN       -29 
6  ac233fc01403  ac233f26492b     e2c56db5       NaN       -31                                                    
7  ac233fc01403  ac233f26492b     e2c56db5       NaN       -30

The resultant outcome should be,

   master_mac    slave_mac     uuid     rawData  rssi            
0  ac233fc01403  ac233f26492b  e2c56db5 NaN      [-23,-28,-32,-37,-29,-31,-30]                                                  
1  ac233fc01403  e464eecba5eb     NaN   590080   [-25]         

Whereas when I use,

df.groupby(['master_mac', 'slave_mac','uuid','rawData'])['rssi'].apply(list)

the same returns,

Series([], Name: rssi, dtype: float64)

While using apply,

df.groupby(['master_mac','slave_mac','uuid','rawData']).apply(lambda x: x['rssi'].values)

it returns as,

Empty DataFrame
Columns: []
Index: []

While using agg,

df.groupby(['master_mac','slave_mac','uuid','rawData']).agg(lambda x: list(x))

returns as,

Empty DataFrame
Columns: []
Index: []

Upvotes: 1

Views: 612

Answers (1)

Pavel Kovtun
Pavel Kovtun

Reputation: 367

try

df.groupby(['master_mac', 'slave_mac','uuid','rawData'])['rssi'].agg(lambda x: list(x))

Upvotes: 1

Related Questions