w3resource

Pandas SQL Query: Display the name, salary and manger id where manager ids are null


10. Display Employees with Null Manager IDs

Write a Pandas program to display the first name, last name, salary and manger id where manager ids are null.

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'].isnull()]
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
Steven          King            24000     nan

Click to view the table contain:

Employees Table

Departments Table

Countries Table

Job_History Table

Jobs Table

Locations Table

Regions Table


For more Practice: Solve these Related Problems:

  • Write a Pandas program to display the first name, last name, salary, and manager id for employees where manager id is null using isnull().
  • Write a Pandas program to filter EMPLOYEES.csv for records with null manager ids and display the specified fields.
  • Write a Pandas program to display the required fields for employees with null manager ids and count the number of such employees.
  • Write a Pandas program to display employees with null manager ids and then sort them by salary in ascending order.

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 first name, last name, salary and department number in descending order by first name.
Next: Write a Pandas program to display the first name, last name, salary and manger id where manager ids are not null.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.