1

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: []
Mahamutha M
  • 1,235
  • 1
  • 8
  • 24
  • Possible duplicate of [How to glue elements into a list when using groupby function?](https://stackoverflow.com/questions/25409958/how-to-glue-elements-into-a-list-when-using-groupby-function) – Georgy Apr 01 '19 at 14:17
  • @Georgy, I have tried with "df.groupby(['master_mac','slave_mac','uuid','rawData']).agg(lambda x: list(x)))" & "df.groupby(['master_mac','slave_mac','uuid','rawData']).apply(lambda x: x['rssi'].values))" whereas both returns an empty dataframe. – Mahamutha M Apr 02 '19 at 11:56
  • 1
    Ah, right, you have `NaN` values. Preprocess your dataframe first: [groupby columns with NaN (missing) values](https://stackoverflow.com/questions/18429491/groupby-columns-with-nan-missing-values) – Georgy Apr 02 '19 at 14:07

1 Answers1

1

try

df.groupby(['master_mac', 'slave_mac','uuid','rawData'])['rssi'].agg(lambda x: list(x))
Pavel Kovtun
  • 367
  • 2
  • 8
  • 2
    While this command may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer) – Popo Apr 01 '19 at 15:39
  • @Paul, agg(lambda x: list(x)) returns an Empty DataFrame Columns: [] Index: [] – Mahamutha M Apr 02 '19 at 11:58