【发布时间】:2021-08-03 06:15:29
【问题描述】:
pandas 中的数据操作存在一些问题,这似乎是 pandas 的错误?会喜欢一些想法。
我有一个(按索引排序的)数据框 my_df,它看起来像这样:
value1 value2
col0 col1 col2 col3 col4
0035ca76-b209-4c4e-9bba-b18459c4dceb 203 positive 173 148 0.0 0.086892
negative 1156 148 0.0 0.090347
1157 148 0.0 0.090347
1158 148 0.0 0.090347
1159 148 0.0 0.084884
1160 148 0.0 0.079942
1161 148 0.0 0.079824
1162 148 0.0 0.071289
positive 173 66 0.0 0.079831
negative 1156 66 0.0 0.082660
1157 66 0.0 0.082660
1158 66 0.0 0.082660
1159 66 0.0 0.084353
1160 66 0.0 0.076934
1161 66 0.0 0.076494
1162 66 0.0 0.070424
00e35aaf-050a-4f09-bf94-df994e4bf681 24 positive 14 38 0.0 0.073936
negative 134 38 0.0 0.075913
135 38 0.0 0.075913
136 38 0.0 0.074403
137 38 0.0 0.081120
138 38 0.0 0.078560
139 38 0.0 0.080680
140 38 0.0 0.073892
positive 14 1 0.0 0.051979
negative 134 1 0.0 0.043818
135 1 0.0 0.043818
136 1 0.0 0.049795
137 1 0.0 0.052171
138 1 0.0 0.048573
139 1 0.0 0.045205
140 1 0.0 0.054696
... more rows for this and other col0 + col1 combos
我正在尝试为 [col0, col1, col2, col3] 的每个唯一组合计算“value2”的总和。据我所知,最合乎逻辑的方法是
my_df.groupby(level=list(range(4))).sum()
但是,我得到了非常奇怪的结果,看起来就像是熊猫错误。
grouped = my_df.groupby(list(range(4)))
for name, group in grouped:
print(group)
break
sums = grouped.sum()
确实第一组如我所料
value1 value2
col0 col1 col2 col3 col4
0035ca76-b209-4c4e-9bba-b18459c4d681 199 positive 174 151 0.0 0.089186
158 0.0 0.104250
grouped 中的组数是正确的(你必须相信我的话,我已经验证了其他方式)但是sums 是混乱的并且有一个 bajillion多余的行
(Pdb) len(grouped)
334
(Pdb) len(sums)
53760
(Pdb) sums[:30]
value1 valu2
col0 col1 col2 col3 col4
1f11aede-6aed-44ef-9296-004b6269662c 17 positive 7 1 0.0 0.0
4 0.0 0.0
5 0.0 0.0
6 0.0 0.0
7 0.0 0.0
8 0.0 0.0
11 0.0 0.0
12 0.0 0.0
24 0.0 0.0
32 0.0 0.0
33 0.0 0.0
38 0.0 0.0
39 0.0 0.0
53 0.0 0.0
56 0.0 0.0
66 0.0 0.0
69 0.0 0.0
70 0.0 0.0
72 0.0 0.0
73 0.0 0.0
75 0.0 0.0
85 0.0 0.0
91 0.0 0.0
94 0.0 0.0
116 0.0 0.0
119 0.0 0.0
col4 中给出的值在整个数据框中变化很大。看起来 groupby + 聚合操作为整个数据帧中的每个 col4 值创建了一个总和行,而不是实际上与每个组相关的 col4 值。换句话说,这些行中的大多数甚至在原始数据框中都没有条目:
(Pdb) my_df.loc[("1f11aede-6aed-44ef-9296-004b6269662c", 17, "positive", 7, 1)]
*** KeyError: ('1f11aede-6aed-44ef-9296-004b6269662c', 17, 'positive', 7, 1)
知道这里发生了什么吗?这些似乎完全脱离了 groupby API 和教程描述的脚本。例如,据我所知 groupby => agg 应该在这里为每个组创建一行。
【问题讨论】:
标签: pandas pandas-groupby