在 Pandas groupby 结果中包含索引
Include indices in Pandas groupby results
使用 Pandas groupby,我可以做这样的事情:
>>> df = pd.DataFrame(
... {
... "A": ["foo", "bar", "bar", "foo", "bar"],
... "B": ["one", "two", "three", "four", "five"],
... }
... )
>>> print(df)
A B
0 foo one
1 bar two
2 bar three
3 foo four
4 bar five
>>> print(df.groupby('A')['B'].unique())
A
bar [two, three, five]
foo [one, four]
Name: B, dtype: object
我正在寻找的是生成索引列表而不是 B 列列表的输出:
A
bar [1, 2, 4]
foo [0, 3]
但是,groupby('A').index.unique() 不起作用。什么语法会为我提供我想要的输出?除了使用 groupby,我非常乐意以其他方式执行此操作,尽管我确实需要在我的实际应用程序中按两列进行分组。
使用df.reset_index
with Groupby.Series.unique
In [530]: df.reset_index().groupby('A')['index'].unique()
Out[530]:
A
bar [1, 2, 4]
foo [0, 3]
Name: index, dtype: object
或:
In [533]: df.reset_index().groupby('A')['index'].agg(list)
Out[533]:
A
bar [1, 2, 4]
foo [0, 3]
Name: index, dtype: object
不一定要在groupby
中有标签,可以使用分组对象。
这样可以实现:
df.index.to_series().groupby(df['A']).unique()
输出:
A
bar [1, 2, 4]
foo [0, 3]
dtype: object
获取唯一 B 值的索引:
df[~df[['A', 'B']].duplicated()].index.to_series().groupby(df['A']).unique()
一种直观的方法是添加一行用于定义新列作为索引,您可以继续使用与您编写的代码相同的代码。
df['index'] = df.index
df.groupby('A')['index'].unique()
结果:
如果您想要 'B' 中唯一值的索引,而不是唯一索引,那么您可以
df.reset_index().groupby('A').apply(lambda g: g.drop_duplicates(['B'])['index'].tolist())
它与@Mayank 和@mozway 的答案不同,当应用于稍微修改过的示例 df:
df = pd.DataFrame(
{
"A": ["foo", "bar", "bar", "foo", "bar", "foo"],
"B": ["one", "two", "three", "four", "five", "one"],
}
)
我的回答是return
A
bar [1, 2, 4]
foo [0, 3]
dtype: object
而@Mayank 和@mozway 会 return
A
bar [1, 2, 4]
foo [0, 3, 5]
Name: index, dtype: object
使用 Pandas groupby,我可以做这样的事情:
>>> df = pd.DataFrame(
... {
... "A": ["foo", "bar", "bar", "foo", "bar"],
... "B": ["one", "two", "three", "four", "five"],
... }
... )
>>> print(df)
A B
0 foo one
1 bar two
2 bar three
3 foo four
4 bar five
>>> print(df.groupby('A')['B'].unique())
A
bar [two, three, five]
foo [one, four]
Name: B, dtype: object
我正在寻找的是生成索引列表而不是 B 列列表的输出:
A
bar [1, 2, 4]
foo [0, 3]
但是,groupby('A').index.unique() 不起作用。什么语法会为我提供我想要的输出?除了使用 groupby,我非常乐意以其他方式执行此操作,尽管我确实需要在我的实际应用程序中按两列进行分组。
使用df.reset_index
with Groupby.Series.unique
In [530]: df.reset_index().groupby('A')['index'].unique()
Out[530]:
A
bar [1, 2, 4]
foo [0, 3]
Name: index, dtype: object
或:
In [533]: df.reset_index().groupby('A')['index'].agg(list)
Out[533]:
A
bar [1, 2, 4]
foo [0, 3]
Name: index, dtype: object
不一定要在groupby
中有标签,可以使用分组对象。
这样可以实现:
df.index.to_series().groupby(df['A']).unique()
输出:
A
bar [1, 2, 4]
foo [0, 3]
dtype: object
获取唯一 B 值的索引:
df[~df[['A', 'B']].duplicated()].index.to_series().groupby(df['A']).unique()
一种直观的方法是添加一行用于定义新列作为索引,您可以继续使用与您编写的代码相同的代码。
df['index'] = df.index
df.groupby('A')['index'].unique()
结果:
如果您想要 'B' 中唯一值的索引,而不是唯一索引,那么您可以
df.reset_index().groupby('A').apply(lambda g: g.drop_duplicates(['B'])['index'].tolist())
它与@Mayank 和@mozway 的答案不同,当应用于稍微修改过的示例 df:
df = pd.DataFrame(
{
"A": ["foo", "bar", "bar", "foo", "bar", "foo"],
"B": ["one", "two", "three", "four", "five", "one"],
}
)
我的回答是return
A
bar [1, 2, 4]
foo [0, 3]
dtype: object
而@Mayank 和@mozway 会 return
A
bar [1, 2, 4]
foo [0, 3, 5]
Name: index, dtype: object