Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

indexing - Find indices of a groupby in pandas

I have this fake dataset:

enter image description here

And I want to find how many times a combination of BirthDate and ZipCode occur, like so:

enter image description here

Now, my question is how can I find the positions in the dataset df where these occurences happen? For example, how can I find the position where 2000101 and 08002 are?

Thanks in advance.

question from:https://stackoverflow.com/questions/66061685/find-indices-of-a-groupby-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use GroupBy.agg if need aggregate for counts and for index values like list to new column Pos:

df1 = (df.reset_index()
         .groupby(['BithDate','ZipCode'])
         .agg(RowNumber=('BithDate','size'), Pos = ('index',list)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...