DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Python Packages for Validating Database Migration Projects
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB
  • Data Analytics Using Python
  • BigQuery DataFrames in Python

Trending

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  1. DZone
  2. Data Engineering
  3. Data
  4. Playing With Pandas DataFrames (With Missing Values Table Example)

Playing With Pandas DataFrames (With Missing Values Table Example)

Sometimes, you may want to concat two dataframes by column base or row base. For this action, you can use the concat function.

By 
Zehra Can user avatar
Zehra Can
·
Updated Feb. 04, 20 · Analysis
Likes (2)
Comment
Save
Tweet
Share
10.9K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes, you may want to concat two dataframes by column base or row base. For this action, you can use the concat function. These can be done by the following code.

Python
 




xxxxxxxxxx
1


 
1
#row base concatenation
2
pd.concat([df1, df2])
3
 
          
4
#column base concatentation
5
pd.concat([df1, df2], axis=1)
6
 
          



For a column base, you have to give axis=1 parameter. I want to explain this functionality by preparing a simple missing values table.

You may also like: PySpark DataFrame Tutorial: Introduction to DataFrames

For this, I will again use the data set I have referred to in my article Pandas Dataframe Functions. First, we have to load the data.

Python
 




xxxxxxxxxx
1


 
1
import pandas as pd
2
import numpy as np
3
 
          
4
#load data 
5
df = pd.read_csv("train.csv")



After loading the data we will calculate some information about the data frame and concat them in a dataframe at the end.

First, we can calculate the null count of each column by the following code and assign it to a variable.

Python
 




xxxxxxxxxx
1


 
1
missing_value_cnt = df.isnull().sum()
2
missing_value_cnt



It will give the following result. This will be our first column:

missing_value code

Python
 




xxxxxxxxxx
1


 
1
percentage = 100 * df.isnull().sum() / len(df)
2
percentage



This calculation will give the percentage of the null values in the total values of the column.

percentage of null values

Lastly, data types can be also added to our missing value table:

Python
 




xxxxxxxxxx
1


 
1
data_types = df.dtypes
2
data_types



percentage dataframe

Three of the data sets give us one-dimensional series data. We will concat them to create our final dataframe:

Python
 




xxxxxxxxxx
1


 
1
missing_values_table = pd.concat([missing_value_cnt, percentage, data_types], axis=1)



missing_values_table

As you can see it has all the values, since we want only missing values, we have to filter the result set. Moreover, there are no column names, to clarify what type of data the column has, we also have to rename all the columns. These can be done by the following codes.

Python
 




xxxxxxxxxx
1


 
1
missing_values_table = mis_val_table.rename(columns = {0 : 'Missing Values', 
2
                                                       1 : 'Percentage',
3
                                                       2 : 'Data Types'})



missing_values_table

And now it is time to filter the data frame just to list the missing values statistics for the loaded data frame. For filtering dataframe, iloc and loc can be used.

Selecting Rows and Columns By Loc and ILoc

Data can be selected from data frames by using loc and iloc options:

Loc is used for selecting rows and columns by index and value label, columns can be selected by column names,

Iloc is used for selecting rows and columns by their indexes.

Here are some examples before continuing our missing tables example.

Python
 




xxxxxxxxxx
1


 
1
#let's call the sample dataframe as df:
2
df.iloc[0] # it gets the first row of the df
3
df.iloc[:0] # it gets the first column of the df  
4
 
          
5
df.iloc[0:3] # first three rows of the df. 
6
df.iloc[,0:2] # first two columns of the df with all rows
7
  



Examples by loc: one of the ways is querying data by loc is using indexes on dataframe

Python
 




xxxxxxxxxx
1


 
1
df.set_index('first_name', inplace=True) # assume the df has first_name column
2
df.loc(['zehra'])



The above query gives the data from df where the first_name column has the "zehra" values. or you can query by conditional expressions.

Python
 




xxxxxxxxxx
1


 
1
df.loc[df['first_name'] == 'zehra', 0:2]



Gets all the rows for the "zehra" value with the first two columns in df.

Here is our final missing values table for our dataframe.

Python
 




xxxxxxxxxx
1


 
1
missing_values_table = missing_values_table[
2
    missing_values_table.iloc[:,1] != 0].sort_values(
3
'Percentage', ascending=False).round(1)
4
missing_values_table



The above code first filters the data based on the Percentage column and then sort by on this column in descending order. The "missing value table" gives you a simple readable table for your data frame missing values.


Further Reading

PySpark Join Explained

Database Pandas Data (computing) Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Python Packages for Validating Database Migration Projects
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB
  • Data Analytics Using Python
  • BigQuery DataFrames in Python

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!