Pandas Tutorial
Pandas Tutorial
net /p/data-science/pandas-complete-tutorial-for-data-science-in-2022
Originally published on Towards AI the World’s Leading AI and Technology News and
Media Company. If you are building an AI-related product or service, we invite you to
consider becoming an AI sponsor. At Towards AI, we help scale AI and technology
startups. Let us help you unleash your technology to the masses.
Pandas is one of the most popular python frameworks among data scientists, data
analytics to machine learning engineers. This framework is an essential tool for data
loading, preprocessing, and analysis.
Before learning Pandas, you must understand what is data frame? Data
Frame is a two-dimensional data structure, like a 2d array, or similar to the
table with rows and columns.
For this article, I am using my dummy online store data set, which is located in my Kaggle
account and GitHub. You can download it from both. Also, I will provide you with all this
exercise notebook on my GitHub account, so feel free to use it.
Table of content
1. Setup
2. Loading Different Data Formats
3. Data Preprocessing
4. Memory Management
5. Data Analysis
6. Data Visualization
7. Final Thought
8. Reference
1. Setup
Import
Before moving on to learn pandas first we need to install them and import them. If you
install Anaconda distributions on your local machine or using Google Colab then pandas
will already be available there, otherwise, you follow this installation process from pandas
official’s website.
# Importing libraries
import numpy as np
import pandas as pd
Default setting of pandas display option there is a limitation of columns and rows displays.
When we need to show more rows or columns then we can use set_option() the function
to display a large number of rows or columns. For this function, we can set any number of
rows and columns values.
# we can set numbers for how many rows and columns will be displayed
pd.set_option('display.min_rows', 10) #default will be 10
pd.set_option('display.max_columns', 20)
Pandas is an easy tool for reading and writing different types of files format. Using these
tools we can load CSV, Excel, Pdf, JSON, HTML, HDF5, SQL, Google BigQuery, etc
file easily.
Here are some methods, I will show you how we can read and write most frequently using
file format.
CSV (comma separated file) is the most popular file format. Reading this file we used the
simply read.csv() function.
df = pd.read_csv('dataset/online_store_customer_data.csv')
df.head(3)
We can add some common parameters to tweak this function. If we need to skip some
first rows in the data frame then we can use skiprows a keyword argument. For example,
If we want to skip the first rows then we use skiprows=2. Similarly, if we don’t want to last
2 rows then we can simply use skipfooter=2 . If we don’t want to load the column header
then we can use header=None .
For reading the CSV file form URL, you can directly pass the link.
When you want to save a data frame on a CSV file you can simply use to.csv() the
function. You also need to pass the file name and it will save that file.
Reading a plain text file, we can use read_csv() the function. In this function, you need to
pass the .txt file name.
To read an Excel file, we should use read_excel() the function of the pandas package. If
we have had multiple sheet names then we can pass the sheet name argument with this
function.
We can save our data frame to an excel file same as a CSV file. You can use to_excel()
function with file name and location.
3. Data preprocessing
Data preprocessing is the process of making raw data to clean data. This is the most
crucial part of data science. In this section, we will explore data first then we remove
unwanted columns, remove duplicates, handle missing data, etc. After this step, we get
clean data from raw data.
After the loading data, the first thing we did to look at our data. For this purpose we use
head() and tail() function. The head function will display the first rows and the tail will be
the last rows. By default, it shows 5 rows. Suppose we want to display the first 3 rows and
the last 6 rows. We can do it this way.
If we want to display sample data then we can use sample() a function with the desired
number of rows. It will show the desired number of random rows. If we want to take 7
samples we need to pass 7 in the sample(7) function.
To display data frames information we can use info() the method. It will display columns
data types, counting each column’s total non-null values and its memory space.
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2512 entries, 0 to 2511
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Transaction_date 2512 non-null object
1 Transaction_ID 2512 non-null int64
2 Gender 2484 non-null object
3 Age 2470 non-null float64
4 Marital_status 2512 non-null object
5 State_names 2512 non-null object
6 Segment 2512 non-null object
7 Employees_status 2486 non-null object
8 Payment_method 2512 non-null object
9 Referal 2357 non-null float64
10 Amount_spent 2270 non-null float64
dtypes: float64(3), int64(1), object(7)
memory usage: 216.0+ KB
Display data types of each column we can use the dtypes attribute. We can add
value_counts() methods in dtypes for showing all data types values counting.
# display datatypes
df.dtypes
Transaction_date object
Transaction_ID int64
Gender object
Age float64
Marital_status object
State_names object
Segment object
Employees_status object
Payment_method object
Referal float64
Amount_spent float64
dtype: object
df.dtypes.value_counts()
object 7
float64 3
int64 1
dtype: int64
To display the number of rows and columns we use the shape attribute. The first number
and last number show the number of rows and columns respectively.
df.shape
(2512, 11)
To display the columns name of our data frame we use the columns attribute.
df.columns
If we want to display single or multiple columns data, simply we need to pass column
names with a data frame. To display multiple columns of data information, we need to
pass the list of columns’ names.
0 19.0
1 49.0
2 63.0
Name: Age, dtype: float64
If we want to display a particular range of rows we can use slicing. For example, if we
want to get 2nd to 6th rows we can simply use df[2:7].
After the explore our datasets may need to clean them for better analysis. Data coming in
from multiple sources so It’s possible to have an error in some values. This is where data
cleaning becomes extremely important. In this section, we will delete unwanted columns,
rename columns, correct appropriate data types, etc.
We can use the drop function to delete unwanted columns from the data frame. Don’t
forget to add inplace = True and axis=1. It will change the value in the data frame.
For changing columns name we can use rename() function with passing columns
dictionary. In a dictionary, we will pass key like an old column name and value as a new
desired column name. For example, now we are going to change Transaction_date and
Gender to Date and Sex.
You may add a new column to an existing pandas data frame just by assigning values to a
new column name. For example, the following code creates a third column named
new_col in df_col data frame:
df_col.head(3)
We can replace the new value with the old, with .loc() the method with help of the
condition. For Example, now we are changing Female to Woman and Male to Man in
Sex column.
df_col.head(3)
# changing Female to Woman and Male to Man in Sex column.
#first argument in loc function is condition and second one is columns
name.
df_col.loc[df_col.Sex == "Female", 'Sex'] = 'Woman'
df_col.loc[df_col.Sex == "Male", 'Sex'] = 'Man'
df_col.head(3)
Now Sex columns values are changed Female to Woman and Male to Man.
Datatype change
When we deal with different types of data types sometimes it’s a tedious task. If we want
to work on a date we must need to change this with the exact date format. Otherwise, we
get the problem. This task is easy on pandas. We can use astype() function to convert one
data type to another.
df_col.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2512 entries, 0 to 2511
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 2512 non-null object
1 Sex 2484 non-null object
2 Age 2470 non-null float64
3 Marital_status 2512 non-null object
4 State_names 2512 non-null object
5 Segment 2512 non-null object
6 Employees_status 2486 non-null object
7 Payment_method 2512 non-null object
8 Referal 2357 non-null float64
9 Amount_spent 2270 non-null float64
10 new_col 2270 non-null float64
dtypes: float64(4), object(7)
memory usage: 216.0+ KB
In our Date columns, it’s object type so now we will convert this to date types, and also we
will convert Referal columns float64 to float32.
df_col.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2512 entries, 0 to 2511
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 2512 non-null datetime64[ns]
1 Sex 2484 non-null object
2 Age 2470 non-null float64
3 Marital_status 2512 non-null object
4 State_names 2512 non-null object
5 Segment 2512 non-null object
6 Employees_status 2486 non-null object
7 Payment_method 2512 non-null object
8 Referal 2357 non-null float32
9 Amount_spent 2270 non-null float64
10 new_col 2270 non-null float64
dtypes: datetime64[ns](1), float32(1), float64(3), object(6)
memory usage: 206.2+ KB
In the data preprocessing part, we need to remove duplicate entries. For different kinds of
reasons sometimes our data frames have multiple duplicate entries. Removing duplicate
entries can be easily done with help of the pandas function. First, we use duplicated()
function for identifying duplicate entries then we use drop_duplicates() for removing them.
12
df.loc[duplicate_value, :]
Handling missing values in the common task in the data preprocessing part. For many
reasons most of the time we will encounter missing values. Without dealing with this we
can’t do the proper model building. For this section first, we will find out missing values
then we decided how to handle them. We can handle this by removing affected columns
or rows or replacing appropriate values there.
For displaying missing values we can use isna() function. Counting total missing values in
each column in ascending order we use .sum() and sort_values(ascending=False)
function.
df.isna().sum().sort_values(ascending=False)
Amount_spent 241
Referal 154
Age 42
Gender 28
Employees_status 26
Transaction_date 0
Marital_status 0
State_names 0
Segment 0
Payment_method 0
dtype: int64
If we have less Nan value then we can delete entire rows by dropna() function. For this
function, we will add columns name in subset parameter.
# df copy to df_copy
df_new = df.copy()
If we have a large number of Nan values in particular columns then dropping those
columns might be a good decision rather than imputing.
df_new.drop(columns=['Amount_spent'], inplace=True)
df_new.isna().sum().sort_values(ascending=False)
Referal 153
Age 42
Gender 27
Transaction_date 0
Marital_status 0
State_names 0
Segment 0
Employees_status 0
Payment_method 0
dtype: int64
Sometimes if we delete entire columns that will be not the appropriate approach. Delete
columns can affect our model building because we will lose our main features. For
imputing we have many approaches so here are some of the most popular techniques.
Method 1 — Impute fixed values like 0, ‘Unknown’ or ‘Missing’ etc. We impute Unknown in
Gender columns
df['Gender'].fillna('Unknown', inplace=True)
Method 3 — Imputing forward fill or backfill by ffill and bfill. In ffill missing value impute
from the value of the above row and for bfill it’s taken from the below rows value.
df['Referal'].fillna(method='ffill', inplace=True)
df.isna().sum().sum()
Now we deal with all missing values with different methods. So now we haven’t any
null values.
4. Memory management
When we work on large datasets, There we get one big issue is a memory problem. We
need too large resources for dealing with this. But there are some methods in pandas to
deal with this. Here are some methods or strategies to deal with this problem with help
of pandas.
Change datatype
From changing one datatype to another we can save lots of memory. One popular trick is
to change objects to the category it will reduce our data frame memory drastically.
First, we will copy our previous df data frame to df_memory and we will calculate the total
memory usage of this data frame using memory_usage(deep=True) method.
df_memory = df.copy()
memory_usage = df_memory.memory_usage(deep=True)
memory_usage_in_mbs = round(np.sum(memory_usage / 1024 ** 2), 3)
print(f" Total memory taking df_memory dataframe is :
{memory_usage_in_mbs:.2f} MB ")
Our data frame is small in size. Which is 1.15 MB. Now We will convert our object
datatype to category.
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2500 entries, 0 to 2511
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Transaction_date 2500 non-null category
1 Gender 2500 non-null category
2 Age 2500 non-null float64
3 Marital_status 2500 non-null category
4 State_names 2500 non-null category
5 Segment 2500 non-null category
6 Employees_status 2500 non-null category
7 Payment_method 2500 non-null category
8 Referal 2500 non-null float64
9 Amount_spent 2500 non-null float64
dtypes: category(7), float64(3)
memory usage: 189.1 KB
Now its reduce 1.15 megabytes to 216.6 KB. It’s almost reduced 5.5 times.
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2500 entries, 0 to 2511
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Transaction_date 2500 non-null category
1 Gender 2500 non-null category
2 Age 2500 non-null float64
3 Marital_status 2500 non-null category
4 State_names 2500 non-null category
5 Segment 2500 non-null category
6 Employees_status 2500 non-null category
7 Payment_method 2500 non-null category
8 Referal 2500 non-null float32
9 Amount_spent 2500 non-null float64
dtypes: category(7), float32(1), float64(2)
memory usage: 179.3 KB
After changing only one column’s data types we reduce 216 KB to 179 KB.
5. Data Analysis
In the data analysis part, we need to calculate some statistical measurements. For
calculating this pandas have multiple useful functions. The first useful function is
describe() the function it will display most of the basic statistical measurements. For this
function, you can add .T for transforming the display. It will make it easy to look at when
there are multiple columns.
df.describe().T
The above function only shows numerical column information. count shows how many
values are there. mean shows the average value of each column. std shows the standard
deviation of columns, which measures the amount of variation or dispersion of a set of
values. min is the minimum value of each column. 25%, 50%, and 75% show total values
lie in that groups, and finally max shows maximum values of that columns.
We know already above code will display only numeric columns basic statistical
information. for object or category columns we can use describe(include=object) .
df.describe(include=object).T
The above information, count shows how many values are there. unique is how many
values are unique in that column. The top is the highest number of values lying in that
category. freq shows how many values frequently lie on that top values.
We can calculate the mean, median, mode, maximum values, minimum values of
individual columns we simply use these functions.
# Calculate Mean
mean = df['Age'].mean()
# Calculate Median
median = df['Age'].median()
#Calculate Mode
mode = df['Age'].mode().iloc[0]
In pandas, we can display the correlation of different numeric columns. For this, we can
use .corr() function.
# calculate correlation
df.corr()
In pandas, there are so many useful basic functions available for data analysis. In this
section, we are exploring some of the most frequently used functions.
Number of unique values in the category column
To display the sum of all unique values we use nunique() the function of desired columns
name. For example, display total unique values in State_names columns we use this
function:
# for display how many unique values are there in State_names column
df['State_names'].nunique()
50
To display all unique values we use unique() function with the desired column name.
To show unique values count we use value_counts() method. This function will display
unique values with a number of each value that occurs. For example, if we want to know
how many unique values of Gender columns with value frequency number of then we use
this method below.
df['Gender'].value_counts()
Female 1351
Male 1121
Unknown 28
Name: Gender, dtype: int64
If we want to show with the percentage of occurrence rather number than we use
normalize=True argument in value_counts() function
Female 0.5404
Male 0.4484
Unknown 0.0112
Name: Gender, dtype: float64
df['State_names'].value_counts().sort_values(ascending = False).head(20)
Illinois 67
Georgia 64
Massachusetts 63
Maine 62
Kentucky 59
Minnesota 59
Delaware 56
Missouri 56
New York 55
New Mexico 55
Arkansas 55
California 55
Arizona 55
Nevada 55
Vermont 54
New Jersey 53
Oregon 53
Florida 53
West Virginia 53
Washington 52
Name: State_names, dtype: int64
Sort values
If we want to sort data frames by particular columns, we need to use sort_values() the
method. We can use sort by ascending or descending order. By default, it’s in ascending
order. If we want to use descending order then simply we need to pass ascending=False
argument in sort_values() the function.
Alternatively, We can use nlargest() and nsmallest() functions for displaying largest and
smallest values with desired numbers. for example, If we want to display the 4 largest
Amount_spent rows then we use this:
# nlargest
df.nlargest(4, 'Amount_spent').head(10) # first argument is how many
rows you want to disply and second one is columns name
# nsmallest
df.nsmallest(3, 'Age').head(10)
Conditional queries on Data
If we want to apply a single condition then first we will give one condition then we pass on
the data frame. For example, if we want to display all rows where Payment_method is
PayPal then we use this:
We can apply multiple conditional queries like before. For example, if we want to display
all Married female people who lived in New York then we use the following:
Group by
In Pandas group by function is more popular in data analysis parts. It allows to split and
group data, apply a function, and combine the results. We can understand this function
and use by below example:
Grouping by one column: For example, if we want to find maximum values of Age and
Amount_spent by Gender then we can use this:
df[['Age', 'Amount_spent']].groupby(df['Gender']).max()
To find mean, count, and max values of Age and Amount_spent by Gender then we can
use agg() function with groupby() .
For creating a simple cross tab between Maritatal_status and Payment_method columns
we just use crosstab() with both column names.
pd.crosstab(df.Marital_status, df.Payment_method)
6. Data Visualization
Visualization is the key to data analysis. The most popular python package for
visualization is matplotlib and seaborn but sometimes pandas will be handy for you.
Pandas also provide some visualization plots easily. For the basic analysis part, it will be
easy to use. For this section, we are exploring some different types of plots using pandas.
Here are the plots.
A line plot is the simplest of all graphical plots. A line plot is utilized to follow changes over
a continuous-time and show information as a series. Line charts are ideal for comparing
multiple variables and visualizing trends for single and multiple variables.
For creating a line plot in pandas we use .plot() two columns’ names for the argument. For
example, we create a line plot from one dummy dataset.
dict_line = {
'year': [2016, 2017, 2018, 2019, 2020, 2021],
'price': [200, 250, 260, 220, 280, 300]
}
df_line = pd.DataFrame(dict_line)
The above line chart shows prices over a different time. It shows like price trend.
A bar plot is also known as a bar chart shows quantitative or qualitative values for different
category items. In a bar, plot data are represented in the form of bars. Bars length or
height are used to represent the quantitative value for each item. Bar plot can be plotted
horizontally or vertically. For creating these plots look below.
df['Employees_status'].value_counts().plot(kind='bar');
For vertical bar:
df['Employees_status'].value_counts().plot(kind='barh');
A pie plot is also known as a pie chart. A pie plot is a circular graph that represents the
total value with its components. The area of a circle represents the total value and the
different sectors of the circle represent the different parts. In this plot, the data are
expressed as percentages. Each component is expressed as a percentage of the
total value.
In pandas for creating pie plot. We use kind=pie in plot() function in data frame column
or series.
df['Segment'].value_counts().plot(
kind='pie');
A box plot is also known as a box and whisker plot. This plot is used to show the
distribution of a variable based on its quartiles. Box plot displays the five-number summary
of a set of data. The five-number summary is the minimum, first quartile, median, third
quartile, and maximum. It will also be popular to identify outliers.
We can plot this by one column or multiple columns. For multiple columns, we need to
pass columns name in y variable as a list.
df.plot(y=['Amount_spent'], kind='box');
In a box plot, we can plot the distribution of categorical variables against a numerical
variable and compare them. Let’s plot it with the Employees_status and Amount_spent
columns with pandas boxplot() method:
np.warnings.filterwarnings('ignore',
category=np.VisibleDeprecationWarning)
fig, ax = plt.subplots(figsize=(6,6))
6.5 Histogram
df.plot(
y='Age',
kind='hist',
bins=10
);
6.6 KDE plot
A kernel density estimate (KDE) plot is a method for visualizing the distribution of
observations in a data set, analogous to a histogram. KDE represents the data using a
continuous probability density curve in one or more dimensions.
df.plot(
y='Age',
xlim=(0, 100),
kind='kde'
);
A scatter plot is used to observe and show relationships between two quantitative
variables for different category items. Each member of the data set gets plotted as a point
whose x-y coordinates relate to its values for the two variables. Below we will plot a
scatter plot to display relationships between Age and Amount_spent columns.
df.plot(
x='Age',
y='Amount_spent',
kind='scatter'
);
7. Final Thoughts
In this article, we know how pandas can be used to read, preprocess, analyze, and
visualize data. It can be also used for memory management for fast computing with fewer
resources. The main motive of this article is to help peoples who are curious to learn
pandas for data analysis.
Do you have any queries or help related to this article please feel free to contact me on
LinkedIn. If you find this article helpful then please follow me for further learning. Your
suggestion and feedback are always welcome. Thank you for reading my article. Have
wonderful learning.
8. Reference
Pandas Complete tutorial for data science in 2022 was originally published in Towards AI
on Medium, where people are continuing the conversation by highlighting and responding
to this story.