I have a pandas data frame and want to return the rows from the data frame corresponding to the client identifiers that appear in the list of target identifiers.
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 rows for clients that appear more than once in this data frame. Therefore, I want to return all identifiers that occur more than once.
id Name ... ... ------------------------- 2 Dave ... ... 2 Dave ... ... 4 Rick ... ... 4 Rick ... ...
I can get a list of identifiers by doing the following
grouped_ids = df.groupby('id').size() id_list = grouped_ids[grouped_ids>1].index.tolist()
And now I would like to go back to the data frame and return all the rows corresponding to these identifiers in the list.
Is it possible?
Thanks for the help.
source share