python - pandas groupby 作为列表返回空,同时使用 agg 和 apply 函数

标签 python pandas list dataframe pandas-groupby

当尝试根据列组(即“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)

使用 apply 时,

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: []

最佳答案

尝试

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

关于python - pandas groupby 作为列表返回空,同时使用 agg 和 apply 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454996/

相关文章:

python - Google App Engine - 跟踪使用了哪些索引

python-3.x - 在 Pandas 时间序列中用 groupby 拟合一条线并获得斜率

html - 是否可以使用 css 将可变宽度的伪元素内容添加到 div 的/左侧?

python - cifar10 随机化训练和测试集

python - 提取具有多个分隔符的文本

python - 来自 Pandas 数据框的烛台图,按日期替换索引

python - 将 GPS 点与 Panda 分组

c# - 连接 byte[] 的 C# 列表

html - 在 Bootstrap 列表中垂直居中文本

python - Pandas 自合并阻塞了缺失的结构