Python Pandas Groupby First-从原始数据帧提取索引

Python Pandas Groupby First-从原始数据帧提取索引,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我有一个非常简单的问题。我想获取一个数据帧,对一些列执行groupby,并提取每个组中第一行的索引(在原始数据帧中)。我该怎么做 我尝试过使用作为索引,分组键,重置索引(),但似乎没有任何效果。您首先需要功能: x = pd.DataFrame([{'name': 'b1', 'group': 'a'}, {'name': 'b2', 'group': 'a'}, {'name': 'b3', 'group': 'a'},

我有一个非常简单的问题。我想获取一个数据帧,对一些列执行groupby,并提取每个组中第一行的索引(在原始数据帧中)。我该怎么做


我尝试过使用
作为索引
分组键
重置索引()
,但似乎没有任何效果。

您首先需要
功能

x = pd.DataFrame([{'name': 'b1', 'group': 'a'},
                  {'name': 'b2', 'group': 'a'},
                  {'name': 'b3', 'group': 'a'},
                  {'name': 'b4', 'group': 'b'},
                  {'name': 'b5', 'group': 'b'},
                  {'name': 'b6', 'group': 'a'},
                  {'name': 'b7', 'group': 'c'},
                  {'name': 'b8', 'group': 'c'},])
x = x.reset_index() # add the indices as a column
xc = x.groupby('group').first()
print(xc)


       index    name
group               
a          0  b1
b          3  b4
c          6  b7

您首先需要函数

x = pd.DataFrame([{'name': 'b1', 'group': 'a'},
                  {'name': 'b2', 'group': 'a'},
                  {'name': 'b3', 'group': 'a'},
                  {'name': 'b4', 'group': 'b'},
                  {'name': 'b5', 'group': 'b'},
                  {'name': 'b6', 'group': 'a'},
                  {'name': 'b7', 'group': 'c'},
                  {'name': 'b8', 'group': 'c'},])
x = x.reset_index() # add the indices as a column
xc = x.groupby('group').first()
print(xc)


       index    name
group               
a          0  b1
b          3  b4
c          6  b7

我误用了
reset\u index()
。谢谢:)我误用了
reset\u index()
。谢谢:)