Pandas SQL Query: Display the name, salary and department number for those employees whose managers are hold the ID 120, 103 or 145
Pandas HR database Queries: Exercise-18 with Solution
Write a Pandas program to display the first name, last name, salary and department number for those employees whose managers are hold the ID 120, 103 or 145.
EMPLOYEES.csv
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 Salary Manager ID")
result = employees[employees['manager_id'].isin([120, 103, 145])]
for index, row in result.iterrows():
print(row['first_name'].ljust(15),row['last_name'].ljust(15),str(row['salary']).ljust(9),row['manager_id'])
Sample Output:
First name Last name Salary Manager ID Bruce Ernst 6000 103.0 David Austin 4800 103.0 Valli Pataballa 4800 103.0 Diana Lorentz 4200 103.0 Julia Nayer 3200 120.0 Irene Mikkilineni 2700 120.0 James Landry 2400 120.0 Steven Markle 2200 120.0 Peter Tucker 10000 145.0 David Bernstein 9500 145.0 Peter Hall 9000 145.0 Christopher Olsen 8000 145.0 Nanette Cambrault 7500 145.0 Oliver Tuvault 7000 145.0 Winston Taylor 3200 120.0 Jean Fleaur 3100 120.0 Martha Sullivan 2500 120.0 Girard Geoni 2800 120.0
Equivalent SQL Syntax:
SELECT first_name, last_name, email, salary, manager_id FROM employees WHERE manager_id IN (120 , 103 , 145);
Click to view the table contain:
Python Code Editor:
Structure of HR database :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:Write a Pandas program to display the first name, last name, salary and department number for employees who works either in department 70 or 90.
Next: Write a Pandas program to display the first, last name, salary and department number for those employees who holds a letter n as a 3rd character in their first name.
What is the difficulty level of this exercise?
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework