I have a pandas data frame and want to return the rows from the data frame corresponding to the customer ids that appear in a list of target ids.
For example, if my data frame looks like this:
id Name ... ...
-------------------------
1 Bob ... ...
2 Dave ... ...
2 Dave ... ...
3 Phil ... ...
4 Rick ... ...
4 Rick ... ...
Basically I want to return the rows for customers who appear more than once in this data frame. So I want to return all the ids that occur more than once.
id Name ... ...
-------------------------
2 Dave ... ...
2 Dave ... ...
4 Rick ... ...
4 Rick ... ...
I can get a list of the ids by doing the following
grouped_ids = df.groupby('id').size()
id_list = grouped_ids[grouped_ids>1].index.tolist()
And now I'd like to go back to the data frame and return all the rows corresponding to those ids in the list.
Is this possible?
Thanks for the help.