jim-the-reporter jim-the-reporter - 1 year ago 106
Python Question

Python pandas: Find values in one column that fall within range in another column

I have a pandas dataframe that contains payroll information from the years 2013 through 2016. Each row describes the amount of money the employee earned in a single year. It looks like this:

Name, Year, Amount

"Bill Smith", "2014", "$20,000"

"John Jones", "2014", "$10,000"

"Bill Smith", "2015", "$21,000"

"John Jones", "2015", "$12,000"

"Sam Stone", "2015", "$15,000"

I need to filter the dataframe to select workers who were hired after 2014 (for example, Sam Stone, but not Bill Smith or John Jones). Any suggestions? My guess is to use groupby() and then try to use conditions to filter the list.

Answer Source

This should work:

workers = df[df.Year<2015].Name.unique()
mew_workers_data = df[~df.Name.isin(workers)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download