【发布时间】:2018-07-30 07:07:41
【问题描述】:
我在 Pandas 中有一个收集数据的数据框;
import pandas as pd
df = pd.DataFrame({'Group': ['A','A','A','A','A','A','A','B','B','B','B','B','B','B'], 'Subgroup': ['Blue', 'Blue','Blue','Red','Red','Red','Red','Blue','Blue','Blue','Blue','Red','Red','Red'],'Obs':[1,2,4,1,2,3,4,1,2,3,6,1,2,3]})
+-------+----------+-----+
| Group | Subgroup | Obs |
+-------+----------+-----+
| A | Blue | 1 |
| A | Blue | 2 |
| A | Blue | 4 |
| A | Red | 1 |
| A | Red | 2 |
| A | Red | 3 |
| A | Red | 4 |
| B | Blue | 1 |
| B | Blue | 2 |
| B | Blue | 3 |
| B | Blue | 6 |
| B | Red | 1 |
| B | Red | 2 |
| B | Red | 3 |
+-------+----------+-----+
观察值 ('Obs') 的编号应该没有间隙,但您可以看到我们在 A 组中“错过”了蓝色 3,在 B 组中“错过”了蓝色 4 和 5。期望的结果是所有 '每个组错过了'观察('Obs'),因此在示例中:
+-------+--------------------+--------+--------+
| Group | Total Observations | Missed | % |
+-------+--------------------+--------+--------+
| A | 8 | 1 | 12.5% |
| B | 9 | 2 | 22.22% |
+-------+--------------------+--------+--------+
我尝试使用 for 循环和使用组(例如:
df.groupby(['Group','Subgroup']).sum()
print(groups.head)
) 但我似乎无法以任何我尝试的方式让它工作。我是不是走错了路?
来自another answer(对@Lie Ryan 大喊大叫)我找到了一个查找缺失元素的函数,但是我还不太明白如何实现它;
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def missing_elements(L):
missing = chain.from_iterable(range(x + 1, y) for x, y in window(L) if (y - x) > 1)
return list(missing)
谁能给我指点一下是正确的方向吗?
【问题讨论】:
标签: python pandas grouping sequence data-science