groupby .indices
时间: 2023-11-09 10:01:59 浏览: 36
groupby.indices 是一个字典,它的键是每个唯一组的值,值是一个数组,包含该组在原始数组中的索引。
例如,对于以下代码:
```
import pandas as pd
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C': [1, 2, 3, 4, 5, 6, 7, 8],
'D': [10, 20, 30, 40, 50, 60, 70, 80]})
grouped = df.groupby(['A', 'B'])
print(grouped.indices)
```
输出结果为:
```
{('bar', 'one'): array([1]), ('bar', 'three'): array([3, 5]), ('bar', 'two'): array([2, 4]), ('foo', 'one'): array([0, 6]), ('foo', 'three'): array([7]), ('foo', 'two'): array([2, 4])}
```
这意味着,对于组合 ('bar', 'one'),在原始 DataFrame 中,第 1 行是该组的唯一成员;对于组合 ('bar', 'three'),在原始 DataFrame 中,第 3 行和第 5 行是该组的成员。