【问题标题】:pandas groupby as list returns empty with both agg and apply functionpandas groupby as list 使用 agg 和 apply 函数返回空
【发布时间】:2019-08-22 14:41:27
【问题描述】:

当尝试根据列组(即“master_mac”和“slave_mac”)将“rssi”列作为列表返回时,pandas 数据框返回空,下面是我的输入数据框

   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

结果应该是,

   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]         

而当我使用时,

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

同样的回报,

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

使用应用时,

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

它返回为,

Empty DataFrame
Columns: []
Index: []

在使用 agg 时,

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

返回为,

Empty DataFrame
Columns: []
Index: []

【问题讨论】:

标签: python pandas list dataframe pandas-groupby


【解决方案1】:

试试

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

【讨论】:

  • 虽然此命令可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。 How to Answer
  • @Paul, agg(lambda x: list(x)) 返回一个空 DataFrame 列:[] 索引:[]
猜你喜欢
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2023-03-24
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多