w3resource
Python Pandas Exercises

Pandas HR database: Display employees who is either Sales Representative or Sales Man

Pandas HR database Queries: Exercise-24 with Solution

Write a Pandas program to display the first and last name and date of joining of the employees who is either Sales Representative or Sales Man.

Sample Solution:

Python Code :

import pandas as pd
employees = pd.read_csv(r"EMPLOYEES.csv")
departments = pd.read_csv(r"DEPARTMENTS.csv")
job_history = pd.read_csv(r"JOB_HISTORY.csv")
jobs = pd.read_csv(r"JOBS.csv")
countries = pd.read_csv(r"COUNTRIES.csv")
regions = pd.read_csv(r"REGIONS.csv")
locations = pd.read_csv(r"LOCATIONS.csv")
print("First name      Last name       Job ID          Hire Date")
result = employees[employees['job_id'].isin(['SA_REP', 'SA_MAN'])]
for index, row in result.iterrows():
    print(row['first_name'].ljust(15),row['last_name'].ljust(15),str(row['job_id']).ljust(15),str(row['hire_date']).ljust(10))

Sample Output:

First name      Last name       Job ID          Hire Date
John            Russell         SA_MAN          1987-08-01
Karen           Partners        SA_MAN          1987-08-02
Alberto         Errazuriz       SA_MAN          1987-08-03
Gerald          Cambrault       SA_MAN          1987-08-04
Eleni           Zlotkey         SA_MAN          1987-08-05
Peter           Tucker          SA_REP          1987-08-06
David           Bernstein       SA_REP          1987-08-07
Peter           Hall            SA_REP          1987-08-08
Christopher     Olsen           SA_REP          1987-08-09
Nanette         Cambrault       SA_REP          1987-08-10
Oliver          Tuvault         SA_REP          1987-08-11
Janette         King            SA_REP          1987-08-12
Patrick         Sully           SA_REP          1987-08-13
Allan           McEwen          SA_REP          1987-08-14
Lindsey         Smith           SA_REP          1987-08-15
Louise          Doran           SA_REP          1987-08-16
Sarath          Sewall          SA_REP          1987-08-17
Clara           Vishney         SA_REP          1987-08-18
Danielle        Greene          SA_REP          1987-08-19
Mattea          Marvins         SA_REP          1987-08-20
David           Lee             SA_REP          1987-08-21
Sundar          Ande            SA_REP          1987-08-22
Amit            Banda           SA_REP          1987-08-23
Lisa            Ozer            SA_REP          1987-08-24
Harrison        Bloom           SA_REP          1987-08-25
Tayler          Fox             SA_REP          1987-08-26
William         Smith           SA_REP          1987-08-27
Elizabeth       Bates           SA_REP          1987-08-28
Sundita         Kumar           SA_REP          1987-08-29
Ellen           Abel            SA_REP          1987-08-30
Alyssa          Hutton          SA_REP          1987-08-31
Jonathon        Taylor          SA_REP          1987-09-01
Jack            Livingston      SA_REP          1987-09-02
Kimberely       Grant           SA_REP          1987-09-03
Charles         Johnson         SA_REP          1987-09-04

Equivalent SQL Syntax:

SELECT first_name, last_name, hire_date
	FROM employees 
		WHERE job_id IN ('SA_REP', 'SA_MAN');

Click to view the table contain:

Employees Table

Departments Table

Countries Table

Job_History Table

Jobs Table

Locations Table

Regions Table

Python Code Editor:


Structure of HR database :

HR database

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to display the details of jobs in descending sequence on job title.

What is the difficulty level of this exercise?