Pandas SQL Query: Create a boolean series, where True for not null and False for null values or missing values in specified column of locations file
Pandas HR database Queries: Exercise-12 with Solution
Write a Pandas program to create and display a boolean series, where True for not null and False for null values or missing values in state_province column of locations file.
LOCATIONS.csv
Sample Solution :
Python Code :
import pandas as pd
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
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("Original data / State Province")
print(locations.state_province)
print("\n\n State Province(Not null / Null Series")
print(locations.state_province.notnull())
Sample Output:
Original data / State Province 0 NaN 1 NaN 2 Tokyo Prefecture 3 NaN 4 Texas 5 California 6 New Jersey 7 Washington 8 Ontario 9 Yukon 10 NaN 11 Maharashtra 12 New South Wales 13 NaN 14 NaN 15 Oxford 16 Manchester 17 Bavaria 18 Sao Paulo 19 Geneve 20 BE 21 Utrecht 22 Distrito Federal, Name: state_province, dtype: object State Province(Not null / Null Series 0 False 1 False 2 True 3 False 4 True 5 True 6 True 7 True 8 True 9 True 10 False 11 True 12 True 13 False 14 False 15 True 16 True 17 True 18 True 19 True 20 True 21 True 22 True Name: state_province, dtype: bool
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 manger id where manager ids are not null.
Next: Write a Pandas program to create a boolean series selecting rows with one or more nulls from locations file.
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