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

Categories

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

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

1 Answer

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)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...