从 pandas 数据框中获取索引列表 [英] Getting lists of indices from pandas dataframe
问题描述
我正在尝试从熊猫数据框中获取索引列表.
I'm trying to get a list of indices out of a pandas dataframe.
首先执行导入.
import pandas as pd
构造一个熊猫数据框.
# Create dataframe
data = {'name': ['Jason', 'Jason', 'Tina', 'Tina', 'Tina', 'Jason', 'Tina'],
'reports': [4, 24, 31, 2, 3, 5, 10],
'coverage': [True, False, False, False, True, True, False]}
df = pd.DataFrame(data)
print(df)
输出:
coverage name reports
0 True Jason 4
1 False Jason 24
2 False Tina 31
3 False Tina 2
4 True Tina 3
5 True Jason 5
6 False Tina 10
当coverage设置为True时,我希望在数据框的左侧具有索引,但是我希望每个名称分别具有此索引.最好在没有显式for循环的情况下执行此操作.
I would like to have the indices on the left of the dataframe when the coverage is set to True, but I would like to have this for every name separately. Preferably do this without an explicit for-loop.
所需的输出是这样的.
Desired output is something like this.
list_Jason = [0, 5]
list_Tina = [4]
尝试的解决方案:我认为我应该使用"groupby",然后访问coverage列.从那里我不知道如何进行.感谢所有帮助.
Attempted solution: I thought I should use 'groupby' and then access the coverage column. From there I don't know how to proceed. All help is appreciated.
df.groupby('name')['coverage']
推荐答案
您要获取每个组的索引.
You want to get the index out for each group.
它存储在groupbydataframe的'groups'属性中.
this is stored in the 'groups' attribute of a groupbydataframe.
#filter for coverage==True
#group by 'name'
#access the 'groups' attribute
by_person = df[df.coverage].groupby('name').groups
将返回:
{'Jason': Int64Index([0, 5], dtype='int64'),
'Tina': Int64Index([4], dtype='int64')}
您可以像常规词典一样从中访问个人:
From which you can access the individuals as you would a regular dictionary:
by_person['Jason']
返回:
Int64Index([0, 5], dtype='int64')
您可以将其视为常规列表.
Which you can treat like a regular list.
这篇关于从 pandas 数据框中获取索引列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!