Pandas Groupby First - Extract Index from Original Dataframe

…衆ロ難τιáo~ 提交于 2021-01-28 20:01:13

问题


I have a very simple problem. I'd like to take a data frame, perform a groupby on some columns, and extract the index (in the original data frame) of the first row in each group. How do I do this?

I've tried playing with as_index, group_keys, reset_index() and nothing seems to work.


回答1:


You need the function first:

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


来源:https://stackoverflow.com/questions/61021026/pandas-groupby-first-extract-index-from-original-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!