Skip to content
Advertisement

Find all ids that have 2 specific values for a one column

I have a dataframe that contains data of employees, their managers and the projects they worked on. The dataframe (a bit simplified) looks like this:

JavaScript

I would like get all employees that have both worked with manager 17 and 18, in this case that would be employee 2 and employee 6.

I know I can write a query to get all employees that worked with either manager 17 or 18 using:

JavaScript

But now I would need to find all employees that have worked with bot, since the combination of a employee – manager can be found multiple times in the dataframe I can’t use a count. I think I would need an self join, but I don’t really know how that can be done in pandas.

Advertisement

Answer

You can use DataFrame.drop_duplicates with DataFrame.pivot and DataFrame.dropna for all EmployeeID exist for both managers:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement