简体   繁体   中英

Get names based on another column in pandas dataframe

I need to get the first and last names of people who work in HR department.


    FirstName   LastName        Year    Department
83  Joe         Faulk           2       Austin Public Library
84  Bryce       Benton          5       HR
85  Sarah       Cronin          7       Austin Public Library
86  Gabriel     Montgomery      2       Austin Resource Recovery
87  Patricia    Genty-Andrade   3       HR

This is my code it shows me error AttributeError: 'DataFrame' object has no attribute 'unique'


names = df.iloc[:, 0:4][df['Department'] == 'HR'].unique()

I need the output to be like this

   FirstName   LastName        Department
0  Joe         Faulk           HR
1  Bryce       Benton          HR
2  Sarah       Cronin          HR
3  Gabriel     Montgomery      HR
4  Patricia    Genty-Andrade   HR

use drop_duplicates instead of unique , as unique is for Series.

df.loc[df['Department'] == 'HR',
       ['FirstName', 'LastName', 'Department']].drop_duplicates()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM