[英]Extracting indices of the dataframe, grouped by a column value
I have a Pandas dataframe which is properly indexed: 我有一个正确索引的熊猫数据框:
delta y
0 0 10
1 1 10
2 1 10
3 1 10
4 0 9
5 1 9
I want to extract the indices conditioned on one column (where delta = 1), but group them by the values of a different column (y). 我想提取以一列(其中delta = 1)为条件的索引,但将它们按另一列(y)的值进行分组。 Ideally, I would like a dictionary of the following form: 理想情况下,我想要以下形式的字典:
{10 : [ 1, 2, 3], 9 : [ 5]}
Filter your df firstly , the we using groupby
and to_dict
首先过滤您的df,我们使用groupby
和to_dict
df[df.delta==1].reset_index().groupby('y').index.apply(list).to_dict()
Out[859]: {9: [5], 10: [1, 2, 3]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.