0% found this document useful (0 votes)
5 views126 pages

Introduction To SQLite in Python

The document provides an introduction to SQLite in Python, highlighting its features such as being serverless, self-contained, and transactional. It explains how to connect to an SQLite database using the sqlite3 module, perform operations like creating tables, inserting data, and retrieving data using SQL commands. Additionally, it covers methods for fetching data and deleting records from tables in SQLite.

Uploaded by

preetiyargol72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views126 pages

Introduction To SQLite in Python

The document provides an introduction to SQLite in Python, highlighting its features such as being serverless, self-contained, and transactional. It explains how to connect to an SQLite database using the sqlite3 module, perform operations like creating tables, inserting data, and retrieving data using SQL commands. Additionally, it covers methods for fetching data and deleting records from tables in SQLite.

Uploaded by

preetiyargol72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to SQLite in Python

Databases offer numerous functionalities by which one can manage


large amounts of information easily over the web and high-volume data
input and output over a typical file such as a text file. SQL is a query
language and is very popular in databases. Many websites use
MySQL. SQLite is a “light” version that works over syntax very much
similar to SQL. SQLite is a self-contained, high-reliability, embedded,
full-featured, public-domain, SQL database engine. It is the most used
database engine on the world wide web. Python has a library to access
SQLite databases, called sqlite3, intended for working with this
database which has been included with Python package since version
2.5. SQLite has the following features.
1. Serverless
2. Self-Contained
3. Zero-Configuration
4. Transactional
5. Single-Database

Serverless

Generally, an RDBMS such as MySQL, PostgreSQL, etc., needs a


separate server process to operate. The applications that want to
access the database server use TCP/IP protocol to send and receive
requests and it is called client/server architecture.
SQLite does not require a server to run. SQLite database is joined with
the application that accesses the database. SQLite database read and
write directly from the database files stored on disk and applications
interact with that SQLite

database.

Self-Contained

SQLite is self-contained means it does not need any external


dependencies like an operating system or external library. This feature
of SQLite help especially in embedded devices like iPhones, Android
phones, game consoles, handheld media players, etc. SQLite is
developed using ANSI-C. The source code is available as a big
sqlite3.c and its header file sqlite3.h. If users want to develop an
application that uses SQLite, users just need to drop these files into
your project and compile it with your code.

Zero-Configuration

SQLite is zero-configuration means no setup or administration needed.


Because of the serverless architecture, you don’t need to “install”
SQLite before using it. There is no server process that needs to be
configured, started, and stopped.

Transactional

SQLite is Transactional means they are atomic, consistent, isolated,


and durable(ACID). All transactions in SQLite are fully ACID-compliant.
In other words, all changes within a transaction take place completely
or not at all even when an unexpected situation like application crash,
power failure, or operating system crash occurs.

Single-Database

SQLite is a single database that means it allows a single database


connection to access multiple database files simultaneously. These
features bring many nice features like joining tables in different
databases or copying data between databases in a single command.
SQLite also uses dynamic types for tables. It means you can store any
value in any column, regardless of the data type.

Understanding of SQLite Module Working in


Python

Python SQLite is used to demonstrate how to develop Python


database applications with the SQLite database. You will learn how to
perform SQLite database operations from Python. SQLite comes built-
in with most of the computers and mobile devices and browsers.
Python’s official sqlite3 module helps us to work with the SQLite
database.
In this diagram, the Python sqlite3 module adheres to Python
Database API Specification v2.0 (PEP 249). PEP 249 provides a SQL
interface that has been designed to encourage and maintain the
similarity between the Python modules that are used to access
databases.

Connecting to the Database


Connecting to the SQLite Database can be established using
the connect() method, passing the name of the database to be
accessed as a parameter. If that database does not exist, then
it’ll be created.
sqliteConnection = [Link]('[Link]')

But what if you want to execute some queries after the


connection is being made. For that, a cursor has to be created
using the cursor() method on the connection instance, which
will execute our SQL queries.
cursor = [Link]()
print('DB Init')
The SQL query to be executed can be written in form of a
string, and then executed by calling the execute() method on
the cursor object. Then, the result can be fetched from the
server by using the fetchall() method, which in this case, is
the SQLite Version Number.
query = 'SQL query;'
[Link](query)
result = [Link]()
print('SQLite Version is {}'.format(result))

Consider the below example where we will connect to an SQLite


database and will run a simple query select sqlite_version(); to
find the version of the SQLite we are using.
Example:
 Python

import sqlite3

try:

# Connect to DB and create a cursor

sqliteConnection = [Link]('[Link]')

cursor = [Link]()

print('DB Init')

# Write a query and execute it with cursor

query = 'select sqlite_version();'

[Link](query)
# Fetch and output result

result = [Link]()

print('SQLite Version is {}'.format(result))

# Close the cursor

[Link]()

# Handle errors

except [Link] as error:

print('Error occurred - ', error)

# Close DB Connection irrespective of success

# or failure

finally:

if sqliteConnection:

[Link]()

print('SQLite Connection closed')

Output:
Python SQLite – Create Table
In this article, we will discuss how can we create tables in the
SQLite database from the Python program using the sqlite3
module.
In SQLite database we use the following syntax to create a
table:
CREATE TABLE database_name.table_name(
column1 datatype PRIMARY KEY(one or
more columns),
column2 datatype,
column3 datatype,
…..
columnN datatype
);
Now we will create a table using Python:
Approach:
Import the required module
 Establish the connection or create a connection object with
the database using the connect() function of the sqlite3
module.
 Create a Cursor object by calling the cursor() method of the
Connection object.
 Form table using the CREATE TABLE statement with
the execute() method of the Cursor class.
Implementation:
 Python3
import sqlite3

# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

# Drop the GEEK table if already exists.

cursor_obj.execute("DROP TABLE IF EXISTS GEEK")

# Creating table

table = """ CREATE TABLE GEEK (

Email VARCHAR(255) NOT NULL,

First_Name CHAR(25) NOT NULL,

Last_Name CHAR(25),

Score INT

); """

cursor_obj.execute(table)

print("Table is Ready")

# Close the connection


connection_obj.close()

Output:

Python SQLite – Insert Data


n this article, we will discuss how can we insert data in a table in the
SQLite database from Python using the sqlite3 module. The
SQL INSERT INTO statement of SQL is used to insert a new row in a
table. There are two ways of using the INSERT INTO statement for
inserting rows:
 Only values: The first method is to specify only the value of data to
be inserted without the column names.
INSERT INTO table_name VALUES (value1, value2, value3,…);
table_name: name of the table.
value1, value2,.. : value of first column, second column,… for the new
record
 Column names and values both: In the second method we will
specify both the columns which we want to fill and their
corresponding values as shown below:
INSERT INTO table_name (column1, column2, column3,..) VALUES
( value1, value2, value3,..);
table_name: name of the table.
column1: name of first column, second column …
value1, value2, value3 : value of first column, second column,… for the
new record
Example 1: Below is a program that depicts how to insert data in an
SQLite table using only values. In the program, we first create a table
named STUDENT and then insert values into it using the 1st syntax of
the INSERT query. Finally, we display the content of the table and
commit it to the database.
 Python3

# Import module

import sqlite3

# Connecting to sqlite

conn = [Link]('[Link]')

# Creating a cursor object using the

# cursor() method

cursor = [Link]()

# Creating table

table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),

SECTION VARCHAR(255));"""

[Link](table)

# Queries to INSERT records.

[Link]('''INSERT INTO STUDENT VALUES ('Raju', '7th', 'A')''')


[Link]('''INSERT INTO STUDENT VALUES ('Shyam', '8th', 'B')''')

[Link]('''INSERT INTO STUDENT VALUES ('Baburao', '9th', 'C')''')

# Display data inserted

print("Data Inserted in the table: ")

data=[Link]('''SELECT * FROM STUDENT''')

for row in data:

print(row)

# Commit your changes in the database

[Link]()

# Closing the connection

[Link]()

Output:

SQLite3:
Example 2: The below program is similar to that of the 1st program,
but we insert values into the table by reordering the names of the
columns with values as in the 2nd syntax.
 Python3

# Import module

import sqlite3

# Connecting to sqlite

conn = [Link]('[Link]')

# Creating a cursor object using the

# cursor() method

cursor = [Link]()

# Creating table

table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),

SECTION VARCHAR(255));"""
[Link](table)

# Queries to INSERT records.

[Link](

'''INSERT INTO STUDENT (CLASS, SECTION, NAME) VALUES ('7th', 'A', 'Raju')''')

[Link](

'''INSERT INTO STUDENT (SECTION, NAME, CLASS) VALUES ('B', 'Shyam', '8th')''')

[Link](

'''INSERT INTO STUDENT (NAME, CLASS, SECTION ) VALUES ('Baburao', '9th',


'C')''')

# Display data inserted

print("Data Inserted in the table: ")

data=[Link]('''SELECT * FROM STUDENT''')

for row in data:

print(row)
# Commit your changes in

# the database

[Link]()

# Closing the connection

[Link]()

Output:

SQLite3:

Python SQLite – Select Data from


Table
In this article, we will discuss, select statement of the Python SQLite
module. This statement is used to retrieve data from an SQLite table
and this returns the data contained in the table.
In SQLite the syntax of Select Statement is:
SELECT * FROM table_name;
* : means all the column from the table
To select specific column replace * with the column name or column
names.
Now we will use the Select statement in the Python program and see
the results:
Demonstration of our GEEK table:

Creating the above table:


Here we are going to create the table using the above approach.

 Python3

import sqlite3

# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')
# cursor object

cursor_obj = connection_obj.cursor()

connection_obj.execute("""CREATE TABLE GEEK(

Email varchar(255),

Name varchar(50),

Score int

);""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk1@[Link]","Geek1",25)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk2@[Link]","Geek2",15)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk3@[Link]","Geek3",36)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk4@[Link]","Geek4",27)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk5@[Link]","Geek5",40)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk6@[Link]","Geek6",36)""")

connection_obj.execute(

"""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk7@[Link]","Geek7",27)""")

connection_obj.commit()

# Close the connection

connection_obj.close()

Read All Rows:

Now we will use the Select statement to retrieve data from the table
and fetch all records. To fetch all records we will use fetchall() method.
Syntax: [Link]()
where, cursor is an object of sqlite3 connection with database.
Code:
 Python3

import sqlite3

# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

# to select all column we will use

statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("All the data")

output = cursor_obj.fetchall()

for row in output:


print(row)

connection_obj.commit()

# Close the connection

connection_obj.close()

Output:

Read Some Rows:

Now we will use the Select statement to retrieve data from the table
and fetch many records not all. To fetch many records we will use
fetchmany() method.
Syntax: [Link](size)
Parameters: size – a limit to fetch records
where, cursor is an object of sqlite3 connection with database.
Code:
 Python3
import sqlite3

# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

# to select all column we will use

statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("Limited data")

output = cursor_obj.fetchmany(5)

for row in output:

print(row)
connection_obj.commit()

# Close the connection

connection_obj.close()

Output:

Read Only one Row:

Now e will use the Select statement to retrieve data from the table and
fetch only one record. To fetch only one record, we will use fetchone()
method.
Syntax: [Link]()
where, cursor is an object of sqlite3 connection with database.

 Python3

import sqlite3
# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

# to select all column we will use

statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("Only one data")

output = cursor_obj.fetchone()

print(output)

connection_obj.commit()
# Close the connection

connection_obj.close()

Output:

Python SQLite – Deleting Data in


Table
In this article, we will discuss how we can delete data in the table in the
SQLite database from the Python program using the sqlite3 module. In
SQLite database we use the following syntax to delete data from a
table:
DELETE FROM table_name [WHERE Clause]
To create the database, we will execute the following code:

 Python3

import sqlite3

# Connecting to sqlite

# connection object
connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

# Drop the GEEK table if already exists.

cursor_obj.execute("DROP TABLE IF EXISTS GEEK")

# Creating table

table = """ CREATE TABLE GEEK (

Email VARCHAR(255) NOT NULL,

Name CHAR(25) NOT NULL,

Score INT

); """

cursor_obj.execute(table)

#inserting data into geek table

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk1@[Link]","Geek1",25)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk2@[Link]","Geek2",15)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk3@[Link]","Geek3",36)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk4@[Link]","Geek4",27)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk5@[Link]","Geek5",40)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk6@[Link]","Geek6",14)""")

connection_obj.execute("""INSERT INTO GEEK (Email,Name,Score) VALUES


("geekk7@[Link]","Geek7",10)""")

connection_obj.commit()

# Close the connection

connection_obj.close()

Output:
Now we will create a python program to delete the row in a table:

Approach:
 Import the required module.
 Establish the connection or create a connection object with the
database using to connect() function of the sqlite3 module.
 Create a Cursor object by calling the cursor() method of the
Connection object.
 Finally, trigger to execute() method on the cursor object, bypassing
a DELETE statement as a parameter to it.

Example 1: (Delete some data)


 Python3

import sqlite3

# Connecting to sqlite

# connection object
connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

#delete data

cursor_obj.execute("DELETE FROM GEEK WHERE Score < 15")

connection_obj.commit()

# Close the connection

connection_obj.close()

Output:
Example 2: (Delete all data)
 Python3

import sqlite3

# Connecting to sqlite

# connection object

connection_obj = [Link]('[Link]')

# cursor object

cursor_obj = connection_obj.cursor()

cursor_obj.execute("SELECT * FROM GEEK")

print(cursor_obj.fetchall())
#delete data

'''It will delete all rows from

the table

'''

cursor_obj.execute("DELETE FROM GEEK")

print()

print("After deleting all rows")

cursor_obj.execute("SELECT * FROM GEEK")

print(cursor_obj.fetchall())

connection_obj.commit()

# Close the connection

connection_obj.close()

Output:
Python SQLite – DROP Table
In this article, we will discuss the DROP command in SQLite using
Python. But first, let’s get a brief about the drop command.
DROP is used to delete the entire database or a table. It deleted both
records in the table along with the table structure.
Syntax: DROP TABLE TABLE_NAME;
For dropping table, we will first create a database and a table in it.
Let’s create a table in the database.

 Python3

# importing sqlite module

import sqlite3

# create connection to the database

# geeks_database

connection = [Link]('geeks_database.db')

# create table named address of customers with

# 4 columns id,name age and address

[Link]('''CREATE TABLE customer_address

(ID INT PRIMARY KEY NOT NULL,


NAME TEXT NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR(50)); ''')

# close the connection

[Link]()

Output:

Now, Insert 5 records into the customer_address table.

 Python3

# importing sqlite module

import sqlite3
# create connection to the database

# geeks_database

connection = [Link]('geeks_database.db')

# insert records into table

[Link](

"INSERT INTO customer_address VALUES (1, 'nikhil teja', 22, 'hyderabad' )")

[Link](

"INSERT INTO customer_address VALUES (2, 'karthik', 25, 'khammam')")

[Link](

"INSERT INTO customer_address VALUES (3, 'sravan', 22, 'ponnur' )")

[Link](

"INSERT INTO customer_address VALUES (4, 'deepika', 25, 'chebrolu' )")

[Link](
"INSERT INTO customer_address VALUES (5, 'jyothika', 22, 'noida')")

# close the connection

[Link]()

Output:

After insertion let’s see how to execute the drop table command.

 Python3

# importing sqlite module

import sqlite3

# create connection to the

# database geek

connection = [Link]('geeks_database.db')
# drop table

[Link]("DROP TABLE customers_address")

print("data dropped successfully")

# close the connection

[Link]()

Output:

NumPy Introduction
What is NumPy?
NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier


transform, and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source


project and you can use it freely.
NumPy stands for Numerical Python.

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow
to process.

NumPy aims to provide an array object that is up to 50x faster than


traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of


supporting functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and
resources are very important.

Data Science: is a branch of computer science where we study how to


store, use and analyze data for deriving information from it.

Why is NumPy Faster Than Lists?


NumPy arrays are stored at one continuous place in memory unlike lists,
so processes can access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized
to work with latest CPU architectures.
Which Language is NumPy written
in?
NumPy is a Python library and is written partially in Python, but most of
the parts that require fast computation are written in C or C++.

Where is the NumPy Codebase?


The source code for NumPy is located at this github
repository [Link]

github: enables many people to work on the same codebase.

NumPy Creating Arrays


Create a NumPy ndarray Object
NumPy is used to work with arrays. The array object in NumPy is
called ndarray.

We can create a NumPy ndarray object by using the array() function.

ExampleGet your own Python Server


import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

print(type(arr))
Try it Yourself »
type(): This built-in Python function tells us the type of the object passed
to it. Like in above code it shows that arr is [Link] type.

To create an ndarray, we can pass a list, tuple or any array-like object


into the array() method, and it will be converted into an ndarray:

Example
Use a tuple to create a NumPy array:

import numpy as np

arr = [Link]((1, 2, 3, 4, 5))

print(arr)

Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).

nested array: are arrays that have arrays as their elements.

0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.

Example
Create a 0-D array with value 42

import numpy as np

arr = [Link](42)

print(arr)
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or
1-D array.

These are the most common and basic arrays.

Example
Create a 1-D array containing the values 1,2,3,4,5:

import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

NumPy has a whole sub module dedicated towards matrix operations


called [Link]

Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

arr = [Link]([[1, 2, 3], [4, 5, 6]])

print(arr)

3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

Example
Create a 3-D array with two 2-D arrays, both containing two arrays with
the values 1,2,3 and 4,5,6:

import numpy as np

arr = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3],


[4, 5, 6]]])

print(arr)

Check Number of Dimensions?


NumPy Arrays provides the ndim attribute that returns an integer that
tells us how many dimensions the array have.

Example
Check how many dimensions the arrays have:

import numpy as np

a = [Link](42)
b = [Link]([1, 2, 3, 4, 5])
c = [Link]([[1, 2, 3], [4, 5, 6]])
d = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print([Link])
print([Link])
print([Link])
print([Link])

Higher Dimensional Arrays


An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by
using the ndmin argument.

Example
Create an array with 5 dimensions and verify that it has 5 dimensions:

import numpy as np

arr = [Link]([1, 2, 3, 4], ndmin=5)

print(arr)
print('number of dimensions :', [Link])

In this array the innermost dimension (5th dim) has 4 elements, the 4th
dim has 1 element that is the vector, the 3rd dim has 1 element that is
the matrix with the vector, the 2nd dim has 1 element that is 3D array
and 1st dim has 1 element that is a 4D array.

Introduction to Pandas
Pandas is an open-source library in Python that is made mainly for
working with relational or labeled data both easily and intuitively. It
provides various data structures and operations for manipulating
numerical data and time series. This library is built on top of
the NumPy library of Python. Pandas is fast and it has high
performance & productivity for users.
History of Pandas Library
Pandas were initially developed by Wes McKinney in 2008 while he
was working at AQR Capital Management. He convinced the AQR to
allow him to open source the Pandas. Another AQR employee, Chang
She, joined as the second major contributor to the library in 2012. Over
time many versions of pandas have been released. The latest version
of the pandas is 1.5.3, released on Jan 18, 2023.
Why Use Pandas?
 Fast and efficient for manipulating and analyzing data.
 Data from different file objects can be easily loaded.
 Flexible reshaping and pivoting of data sets
 Provides time-series functionality.

What can you do using Pandas?


Pandas are generally used for data science but have you wondered
why? This is because pandas are used in conjunction with other
libraries that are used for data science. It is built on the top of
the NumPy library which means that a lot of structures of NumPy are
used or replicated in Pandas. The data produced by Pandas are often
used as input for plotting functions of Matplotlib, statistical analysis
in SciPy, and machine learning algorithms in Scikit-learn. Here is a
list of things that we can do using Pandas.
 Data set cleaning, merging, and joining.
 Easy handling of missing data (represented as NaN) in floating point
as well as non-floating point data.
 Columns can be inserted and deleted from DataFrame and higher
dimensional objects.
 Powerful group by functionality for performing split-apply-combine
operations on data sets.
 Data Visulaization

Getting Started

Installing Pandas
The first step of working in pandas is to ensure whether it is installed in
the system or not. If not then we need to install it in our system
using the pip command. Type the cmd command in the search box
and locate the folder using the cd command where python-pip file has
been installed. After locating it, type the command:
pip install pandas

Importing Pandas
After the pandas have been installed into the system, you need to
import the library. This module is generally imported as follows:
import pandas as pd
Here, pd is referred to as an alias to the Pandas. However, it is not
necessary to import the library using the alias, it just helps in writing
less amount code every time a method or property is called.

Pandas Data Structures


Pandas generally provide two data structures for manipulating data,
They are:
 Series
 DataFrame

Series
Pandas Series is a one-dimensional labeled array capable of holding
data of any type (integer, string, float, python objects, etc.). The axis
labels are collectively called indexes.
Pandas Series is nothing but a column in an Excel sheet. Labels need
not be unique but must be a hashable type. The object supports both
integer and label-based indexing and provides a host of methods for
performing operations involving the index.

Series Data Frame

Creating a Series
In the real world, a Pandas Series will be created by loading the
datasets from existing storage, storage can be SQL Database, CSV
file, or an Excel file. Pandas Series can be created from lists,
dictionaries, and from scalar values, etc.
Example:
 Python3
import pandas as pd

import numpy as np

# Creating empty series

ser = [Link]()

print("Pandas Series: ", ser)

# simple array

data = [Link](['g', 'e', 'e', 'k', 's'])

ser = [Link](data)

print("Pandas Series:\n", ser)

Output:
Pandas Series: Series([], dtype: float64)
Pandas Series:
0 g
1 e
2 e
3 k
4 s
dtype: object

DataFrame
Pandas DataFrame is a two-dimensional size-mutable, potentially
heterogeneous tabular data structure with labeled axes (rows and
columns). A Data frame is a two-dimensional data structure, i.e., data
is aligned in a tabular fashion in rows and columns. Pandas
DataFrame consists of three principal components, the data, rows, and
columns.

Creating Data Frame


In the real world, a Pandas DataFrame will be created by loading the
datasets from existing storage, storage can be SQL Database, CSV
file, or an Excel file. Pandas DataFrame can be created from lists,
dictionaries, and from a list of dictionaries, etc.
Example:
 Python3

import pandas as pd
# Calling DataFrame constructor

df = [Link]()

print(df)

# list of strings

lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list

df = [Link](lst)

print(df)

Output:
Empty DataFrame
Columns: []
Index: []
0
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks

How to run Pandas Program in Python?


Pandas program can be run from any text editor but it is recommended
to use Jupyter Notebook for this as Jupyter gives the ability to execute
code in a particular cell rather than executing the entire file. Jupyter
also provides an easy way to visualize pandas data frames and plots.
Creating a dataframe from Pandas
series
Series is a type of list in Pandas that can take integer values,
string values, double values, and more. But in Pandas Series we
return an object in the form of a list, having an index starting
from 0 to n, Where n is the length of values in the series. Later
in this article, we will discuss Dataframes in pandas, but we first
need to understand the main difference
between Series and Dataframe. Series can only contain a single
list with an index, whereas Dataframe can be made of more
than one series or we can say that a Dataframe is a collection
of series that can be used to analyze the data.

Creating Pandas DataFrames from Series


 Python3

# importing pandas library

import pandas as pd

# Creating a list

author = ['Jitender', 'Purnima',

'Arpit', 'Jyoti']
# Creating a Series by passing list

# variable to Series() function

auth_series = [Link](author)

# Printing Series

print(auth_series)

Output:
0 Jitender
1 Purnima
2 Arpit
3 Jyoti
dtype: object

Let’s check the type of Series:

 Python3

print(type(auth_series))

Output:
<class '[Link]'>

Create DataFrame From Multiple Series


We have created two lists ‘author’ and article’ which have been
passed to [Link]() functions to create two Series. After
creating the Series, we created a dictionary and passed Series
objects as values of the dictionary, and the keys of the
dictionary will be served as Columns of the Dataframe.
 Python3
# Importing Pandas library

import pandas as pd

# Creating two lists

author = ['Jitender', 'Purnima',

'Arpit', 'Jyoti']

article = [210, 211, 114, 178]

# Creating two Series by passing lists

auth_series = [Link](author)

article_series = [Link](article)

# Creating a dictionary by passing Series objects as values

frame = {'Author': auth_series,

'Article': article_series}

# Creating DataFrame by passing Dictionary

result = [Link](frame)
# Printing elements of Dataframe

print(result)

Output:
Author Article
0 Jitender 210
1 Purnima 211
2 Arpit 114
3 Jyoti 178

Add a Column in Pandas Dataframe


We have added one more series externally named as the age of
the authors, then directly added this series in the Pandas
Dataframe.

 Python3

# Importing pandas library

import pandas as pd

# Creating Series

auth_series = [Link](['Jitender',

'Purnima', 'Arpit', 'Jyoti'])

article_series = [Link]([210, 211, 114, 178])

# Creating Dictionary
frame = {'Author': auth_series,

'Article': article_series}

# Creating Dataframe

result = [Link](frame)

# Creating another list

age = [21, 21, 24, 23]

# Creating new column in the dataframe by

# providing s Series created using list

result['Age'] = [Link](age)

# Printing dataframe

print(result)

Output:
Author Article Age
0 Jitender 210 21
1 Purnima 211 21
2 Arpit 114 24
3 Jyoti 178 23

Missing value in Pandas Dataframe


Remember one thing if any value is missing then by default it
will be converted into NaN value, i.e, null by default.

 Python3
# Importing pandas library

import pandas as pd

# Creating Series

auth_series = [Link](['Jitender',

'Purnima', 'Arpit', 'Jyoti'])

article_series = [Link]([210, 211, 114, 178])

# Creating Dictionary

frame = {'Author': auth_series,

'Article': article_series}

# Creating Dataframe

result = [Link](frame)

# Creating another list

age = [21, 21, 24]

# Creating new column in the dataframe by

# providing s Series created using list

result['Age'] = [Link](age)

# Printing dataframe

print(result)

Output:
Author Article Age
0 Jitender 210 21.0
1 Purnima 211 21.0
2 Arpit 114 23.0
3 Jyoti 178 NaN

Creating a Dataframe using a dictionary of Series


Here, we have passed a dictionary that has been created using
a series as values then passed this dictionary to create a
Dataframe. We can see while creating a Dataframe using
Python Dictionary, the keys of the dictionary will become
Columns and values will become Rows.

 Python3

# Importing pandas library

import pandas as pd

# Creating dictionary of Series

dict1 = {'Auth_Name': [Link](['Jitender',

'Purnima', 'Arpit', 'Jyoti']),

'Author_Book_No':\

[Link]([210, 211, 114, 178]),

'Age': [Link]([21, 21, 24, 23])}

# Creating Dataframe

df = [Link](dict1)
# Printing dataframe

print(df)

Output:
Auth_Name Auth_Book_No Age
0 Jitender 210 21
1 Purnima 211 21
2 Arpit 114 24
3 Jyoti 178 23

Explicit Indexing in Pandas Dataframe


Here we can see after providing an index to the dataframe
explicitly, it has filled all data with NaN values since we have
created this dataframe using Series and Series has its own
default indices(0,1,2) which is why when indices of both
dataframe and Series do not match, we got all NaN values.

 Python3

# Importing pandas library

import pandas as pd

# Creating dictionary of Series

dict1 = {'Auth_Name': [Link](['Jitender',

'Purnima', 'Arpit', 'Jyoti']),

'Author_Book_No': [Link]([210, 211, 114, 178]),

'Age': [Link]([21, 21, 24, 23])}


# Creating Dataframe

df = [Link](dict1, index=['SNo1', 'SNo2', 'SNo3', 'SNo4'])

# Printing dataframe

print(df)

Output:
Auth_Name Author_Book_No Age
SNo1 NaN NaN NaN
SNo2 NaN NaN NaN
SNo3 NaN NaN NaN
SNo4 NaN NaN NaN
Here, we can rectify this problem by providing the same index
values to every Series element.

 Python3

# This code is provided by Sheetal Verma

# Importing pandas library

import pandas as pd

# Creating dictionary of Series

dict1 = {'Auth_Name': [Link](['Jitender',

'Purnima', 'Arpit', 'Jyoti'],

index=['SNo1', 'SNo2', 'SNo3', 'SNo4']),

'Author_Book_No': [Link]([210, 211, 114, 178],


index=['SNo1', 'SNo2', 'SNo3', 'SNo4']),

'Age': [Link]([21, 21, 24, 23],

index=['SNo1', 'SNo2', 'SNo3', 'SNo4'])}

# Creating Dataframe

df = [Link](dict1, index=['SNo1', 'SNo2', 'SNo3', 'SNo4'])

# Printing dataframe

print(df)

Output:
Auth_Name Author_Book_No Age
SNo1 Jitender 210 21
SNo2 Purnima 211 21
SNo3 Arpit 114 24
SNo4 Jyoti 178 23

Creating a dataframe using CSV


files
CSV files are the “comma-separated values”, these values are
separated by commas, this file can be viewed like an excel file. In
Python, Pandas is the most important library coming to data science.
We need to deal with huge datasets while analyzing the data, which
usually can get in CSV file format. Creating a pandas data frame using
CSV files can be achieved in multiple ways.
Note: Get the csv file used in the below examples from here.
Method #1: Using read_csv() method: read_csv() is an important
pandas function to read csv files and do operations on it.
Example:

 Python3

# Python program to illustrate

# creating a data frame using CSV files

# import pandas module

import pandas as pd

# creating a data frame

df = pd.read_csv("[Link]")

print([Link]())

Output:

Method #2: Using read_table() method: read_table() is another


important pandas function to read csv files and create data frame from
it.
Example:
 Python3

# Python program to illustrate

# creating a data frame using CSV files

# import pandas module

import pandas as pd

# creating a data frame

df = pd.read_table("[Link]", delimiter =", ")

print([Link]())

Output:

Method #3: Using the csv module: One can directly import the csv
files using the csv module and then create a data frame using that csv
file.
Example:

 Python3
# Python program to illustrate

# creating a data frame using CSV files

# import pandas module

import pandas as pd

# import csv module

import csv

with open("[Link]") as csv_file:

# read the csv file

csv_reader = [Link](csv_file)

# now we can use this csv files into the pandas

df = [Link]([csv_reader], index = None)

# iterating values of first column

for val in list(df[1]):

print(val)
Output:

['TM195', '18', 'Male', '14', 'Single', '3', '4', '29562',


'112']

DataFrame Operations in R
DataFrames are generic data objects of R which are used to store the
tabular data. Data frames are considered to be the most popular data
objects in R programming because it is more comfortable to analyze
the data in the tabular form. Data frames can also be taught as
mattresses where each column of a matrix can be of the different data
types. DataFrame are made up of three principal components, the
data, rows, and columns.

Operations that can be performed on a DataFrame are:


 Creating a DataFrame
 Accessing rows and columns
 Selecting the subset of the data frame
 Editing dataframes
 Adding extra rows and columns to the data frame
 Add new variables to dataframe based on existing ones
 Delete rows and columns in a data frame

Creating a DataFrame
In the real world, a DataFrame will be created by loading the datasets
from existing storage, storage can be SQL Database, CSV file, and an
Excel file. DataFrame can also be created from the vectors in R.
Following are some of the various ways that can be used to create a
DataFrame:
Creating a data frame using Vectors: To create a data frame we use
the [Link]() function in R. To create a data frame
use [Link]() command and then pass each of the vectors you
have created as arguments to the function.
Example:
 Python3

# R program to illustrate dataframe

# A vector which is a character vector

Name = c("Amiya", "Raj", "Asish")

# A vector which is a character vector

Language = c("R", "Python", "Java")


# A vector which is a numeric vector

Age = c(22, 25, 45)

# To create dataframe use [Link] command and

# then pass each of the vectors

# we have created as arguments

# to the function [Link]()

df = [Link](Name, Language, Age)

print(df)

Output:
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

Creating a data frame using data from a file: Dataframes can also
be created by importing the data from a file. For this, you have to use
the function called ‘[Link]()‘.
Syntax:
newDF = [Link](path="Path of the file")
To create a dataframe from a CSV file in R:
Syntax:
newDF = [Link]("[Link]")
Accessing rows and columns
The syntax for accessing rows and columns is given below,
df[val1, val2]

df = dataframe object
val1 = rows of a data frame
val2 = columns of a data frame

So, this ‘val1‘ and ‘val2‘ can be an array of values such as “1:2” or
“2:3” etc. If you specify only df[val2] this refers to the set of columns
only, that you need to access from the data frame.
Example: Row selection
 Python3

# R program to illustrate operations

# on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)


)

print(df)

# Accessing first and second row

cat("Accessing first and second row\n")

print(df[1:2, ])

Output:
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

Accessing first and second row


Name Language Age
1 Amiya R 22
2 Raj Python 25

Example: Column selection


 Python3

# R program to illustrate operations

# on a data frame
# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

print(df)

# Accessing first and second column

cat("Accessing first and second column\n")

print(df[, 1:2])

Output:
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

Accessing first and second column


Name Language
1 Amiya R
2 Raj Python
3 Asish Java

Selecting the subset of the DataFrame


A subset of a DataFrame can also be created based on certain
conditions with the help of following syntax.
newDF = subset(df, conditions)
df = Original dataframe
conditions = Certain conditions

Example:
 Python3

# R program to illustrate operations

# on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

print(df)
# Selecting the subset of the data frame

# where Name is equal to Amiya

# OR age is greater than 30

newDf = subset(df, Name =="Amiya"|Age>30)

cat("After Selecting the subset of the data frame\n")

print(newDf)

Output:
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

After Selecting the subset of the data frame


Name Language Age
1 Amiya R 22
3 Asish Java 45

Editing DataFrames
In R, DataFrames can be edited in two ways:
Editing data frames by direct assignments: Much like the list in R
you can edit the data frames by a direct assignment.
Example:
 Python3

# R program to illustrate operation on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

cat("Before editing the dataframe\n")

print(df)

# Editing dataframes by direct assignments

# [[3]] accessing the top level components

# Here Age in this case

# [[3]][3] accessing inner level components

# Here Age of Asish in this case


df[[3]][3] = 30

cat("After edited the dataframe\n")

print(df)

Output:
Before editing the data frame
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

After edited the data frame


Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 30

Editing dataframes using the edit() command:


Follow the given steps to edit a DataFrame:
Step 1: So, what you need to do for this is you have to create an
instance of data frame, for example, you can see that here an instance
of a data frame is created and named as “myTable” by using the
command [Link]() and this creates an empty data frame.
myTable = [Link]()

Step 2: Next we will use the edit function to launch the viewer. Note
that “myTable” data frame is passed back to the “myTable” object and
this way the changes we make to this module will be saved to the
original object.
myTable = edit(myTable)

So, when the above command is executed it will pop up a window like
this,

Step 3: Now, the table is completed with this small roster.

Note that, change variable names by clicking on their labels and typing
your changes. Variables can also be set as numeric or character. Once
the data in the DataFrame looks like the above, close the table.
Changes are saved automatically.
Step 4: Check out the resulting data frame by printing it.
> myTable
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

Adding rows and columns to the data frame

Adding extra rows: We can add extra row using the


command rbind(). The syntax for this is given below,
newDF = rbind(df, the entries for the new row you have to add )
df = Original data frame

Note that the entries for the new row you have to add you have to be
careful when using rbind() because the data types in each column
entry should be equal to the data types that are already existing rows.
Example:
 Python3

# R program to illustrate operation on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)


)

cat("Before adding row\n")

print(df)

# Add a new row using rbind()

newDf = rbind(df, [Link](Name = "Sandeep",

Language = "C",

Age = 23

))

cat("After Added a row\n")

print(newDf)

Output:
Before adding row
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

After Added a row


Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
4 Sandeep C 23

Adding extra columns: We can add extra column using the


command cbind(). The syntax for this is given below,
newDF = cbind(df, the entries for the new column you have to add )
df = Original data frame

Example:
 Python3

# R program to illustrate operation on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

cat("Before adding column\n")

print(df)

# Add a new column using cbind()


newDf = cbind(df, Rank=c(3, 5, 1))

cat("After Added a column\n")

print(newDf)

Output:
Before adding column
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

After Added a column


Name Language Age Rank
1 Amiya R 22 3
2 Raj Python 25 5
3 Asish Java 45 1

Adding new variables to DataFrame


In R, we can add new variables to a data frame based on existing
ones. To do that we have to first call the dplyr library using the
command library() . And then calling mutate() function will add extra
variable columns based on existing ones.
Syntax:
library(dplyr)
newDF = mutate(df, new_var=[existing_var])
df = original data frame
new_var = Name of the new variable
existing_var = The modify action you are taking(e.g log value, multiply
by 10)

Example:
 Python3

# R program to illustrate operation on a data frame

# Importing the dplyr library

library(dplyr)

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

cat("Original Dataframe\n")

print(df)
# Creating an extra variable column

# "log_Age" which is log of variable column "Age"

# Using mutate() command

newDf = mutate(df, log_Age = log(Age))

cat("After creating extra variable column\n")

print(newDf)

Output:
Original Dataframe
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45

After creating extra variable column


Name Language Age log_Age
1 Amiya R 22 3.091042
2 Raj Python 25 3.218876
3 Asish Java 45 3.806662
Deleting rows and columns from a data frame
To delete a row or a column, first of all, you need to access that row or
column and then insert a negative sign before that row or column. It
indicates that you had to delete that row or column.

Syntax:
newDF = df[-rowNo, -colNo]
df = original data frame

Example:
 Python3

# R program to illustrate operation on a data frame

# Creating a dataframe

df = [Link](

"Name" = c("Amiya", "Raj", "Asish"),

"Language" = c("R", "Python", "Java"),

"Age" = c(22, 25, 45)

cat("Before deleting the 3rd row and 2nd column\n")

print(df)
# delete the third row and the second column

newDF = df[-3, -2]

cat("After Deleted the 3rd row and 2nd column\n")

print(newDF)

Output:
Before deleting the 3rd row and 2nd column
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Deleted the 3rd row and 2nd column
Name Age
1 Amiya 22
2 Raj 25

Data Visualization with Python


n today’s world, a lot of data is being generated on a daily basis. And
sometimes to analyze this data for certain trends, patterns may become
difficult if the data is in its raw format. To overcome this data visualization
comes into play. Data visualization provides a good, organized pictorial
representation of the data which makes it easier to understand, observe,
analyze. In this tutorial, we will discuss how to visualize data using Python.
Python provides various libraries that come with different features for
visualizing data. All these libraries come with different features and can
support various types of graphs. In this tutorial, we will be discussing four
such libraries.

 Matplotlib
 Seaborn
 Bokeh
 Plotly

We will discuss these libraries one by one and will plot some most commonly
used graphs.

Note: If you want to learn in-depth information about these libraries you can
follow their complete tutorial.

Before diving into these libraries, at first, we will need a database to plot the
data. We will be using the tips database for this complete tutorial. Let’s
discuss see a brief about this database.

Database Used
Tips Database

Tips database is the record of the tip given by the customers in a restaurant
for two and a half months in the early 1990s. It contains 6 columns such as
total_bill, tip, sex, smoker, day, time, size.

You can download the tips database from here.

Example:

Python3

import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

# printing the top 10 rows


display([Link](10))
Output:
Matplotlib
Matplotlib is an easy-to-use, low-level data visualization library that is built on
NumPy arrays. It consists of various plots like scatter plot, line plot, histogram,
etc. Matplotlib provides a lot of flexibility.

To install this type the below command in the terminal.


Refer to the below articles to get more information setting up an environment
with Matplotlib.

 Environment Setup for Matplotlib


 Using Matplotlib with Jupyter Notebook

After installing Matplotlib, let’s see the most commonly used plots using this
library.

Scatter Plot

Scatter plots are used to observe relationships between variables and uses
dots to represent the relationship between them. The scatter() method in the
matplotlib library is used to draw a scatter plot.

Example:

Python3
# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd
# instantiating the figure object
graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'], color=color)

# displaying the model


show(graph)
# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'], color=color)

# displaying the model


show(graph)
# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'], color=color)
# displaying the model
show(graph)
# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'], color=color)

# displaying the model


show(graph)

import pandas as pd
import [Link] as plt
# reading the database
data = pd.read_csv("[Link]")

# Scatter plot with day against tip


[Link](data['day'], data['tip'])

# Adding Title to the Plot


[Link]("Scatter Plot")

# Setting the X and Y labels


[Link]('Day')
[Link]('Tip')

[Link]()
Output:

This graph can be more meaningful if we can add colors and also change the
size of the points. We can do this by using the c and
s parameter respectively of the scatter function. We can also show the color
bar using the colorbar() method.

Example:

Python3
import pandas as pd
import [Link] as plt

# reading the database


data = pd.read_csv("[Link]")

# Scatter plot with day against tip


[Link](data['day'], data['tip'], c=data['size'],
s=data['total_bill'])

# Adding Title to the Plot


[Link]("Scatter Plot")

# Setting the X and Y labels


[Link]('Day')
[Link]('Tip')

[Link]()

[Link]()
Output:
Line Chart

Line Chart is used to represent a relationship between two data X and Y on a


different axis. It is plotted using the plot() function. Let’s see the below
example.

Example:

Python3
import pandas as pd
import [Link] as plt

# reading the database


data = pd.read_csv("[Link]")

# Scatter plot with day against tip


[Link](data['tip'])
[Link](data['size'])

# Adding Title to the Plot


[Link]("Scatter Plot")

# Setting the X and Y labels


[Link]('Day')
[Link]('Tip')

[Link]()
Output:

Bar Chart

A bar plot or bar chart is a graph that represents the category of data with
rectangular bars with lengths and heights that is proportional to the values
which they represent. It can be created using the bar() method.

Example:

Python3
import pandas as pd
import [Link] as plt

# reading the database


data = pd.read_csv("[Link]")

# Bar chart with day against tip


[Link](data['day'], data['tip'])

[Link]("Bar Chart")

# Setting the X and Y labels


[Link]('Day')
[Link]('Tip')

# Adding the legends


[Link]()
Output:
Histogram

A histogram is basically used to represent data in the form of some groups. It


is a type of bar plot where the X-axis represents the bin ranges while the Y-
axis gives information about frequency. The hist() function is used to compute
and create a histogram. In histogram, if we pass categorical data then it will
automatically compute the frequency of that data i.e. how often each value
occurred.

Example:

Python3

import pandas as pd
import [Link] as plt
# reading the database
data = pd.read_csv("[Link]")

# histogram of total_bills
[Link](data['total_bill'])

[Link]("Histogram")

# Adding the legends


[Link]()
Output:

Note: For complete Matplotlib Tutorial, refer Matplotlib Tutorial

Seaborn
Seaborn is a high-level interface built on top of the Matplotlib. It provides
beautiful design styles and color palettes to make more attractive graphs.

To install seaborn type the below command in the terminal.


Seaborn is built on the top of Matplotlib, therefore it can be used with the
Matplotlib as well. Using both Matplotlib and Seaborn together is a very simple
process. We just have to invoke the Seaborn Plotting function as normal, and
then we can use Matplotlib’s customization function.

Note: Seaborn comes loaded with dataset such as tips, iris, etc. but for the
sake of this tutorial we will use Pandas for loading these datasets.

Example:

Python3
# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

# draw lineplot
[Link](x="sex", y="total_bill", data=data)

# setting the title using Matplotlib


[Link]('Title using Matplotlib Function')

[Link]()
Output:
Scatter Plot

Scatter plot is plotted using the scatterplot() method. This is similar to


Matplotlib, but additional argument data is required.

Example:

Python3

# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

[Link](x='day', y='tip', data=data,)


[Link]()
Output:

You will find that while using Matplotlib it will a lot difficult if you want to color
each point of this plot according to the sex. But in scatter plot it can be done
with the help of hue argument.

Example:

Python3

# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

[Link](x='day', y='tip', data=data,


hue='sex')
[Link]()
Output:

Line Plot

Line Plot in Seaborn plotted using the lineplot() method. In this, we can pass
only the data argument also.

Example:

Python3
# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

[Link](x='day', y='tip', data=data)


[Link]()
Output:

Example 2:
Python3

# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

# using only data attribute


[Link](data=[Link](['total_bill'], axis=1))
[Link]()
Output:
Bar Plot

Bar Plot in Seaborn can be created using the barplot() method.

Example:

Python3

# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

[Link](x='day',y='tip', data=data,
hue='sex')

[Link]()
Output:
Histogram

The histogram in Seaborn can be plotted using the histplot() function.

Example:

Python3

# importing packages
import seaborn as sns
import [Link] as plt
import pandas as pd

# reading the database


data = pd.read_csv("[Link]")

[Link](x='total_bill', data=data, kde=True, hue='sex')

[Link]()
Output:

After going through all these plots you must have noticed that customizing
plots using Seaborn is a lot more easier than using Matplotlib. And it is also
built over matplotlib then we can also use matplotlib functions while using
Seaborn.

Scatter Plot

Scatter Plot in Bokeh can be plotted using the scatter() method of the plotting
module. Here pass the x and y coordinates respectively.

Example:

Python3
# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)

# displaying the model


# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd
# instantiating the figure object
graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)

# displaying the model


# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)

# displaying the model


# importing the modules
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")
# reading the database
data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color

Output:

Line Chart

A line plot can be created using the line() method of the plotting module.

Example:

# importing the modules


from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd
# instantiating the figure object
graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)

from [Link] import figure, output_file, show


from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)
# plotting the graph
[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
from [Link] import figure, output_file, show
from [Link] import magma
import pandas as pd

# instantiating the figure object


graph = figure(title = "Bokeh Scatter Graph")

# reading the database


data = pd.read_csv("[Link]")

color = magma(256)

# plotting the graph


[Link](data['total_bill'], data['tip'],
color=color)
Python3

Output:

Bar Chart
Bar Chart can be of two types horizontal bars and vertical bars. Each can be
created using the hbar() and vbar() functions of the plotting interface
respectively.

Example:

Python3

Output:
Interactive Data Visualization

One of the key features of Bokeh is to add interaction to the plots. Let’s see
various interactions that can be added.

Interactive Legends
click_policy property makes the legend interactive. There are two types of
interactivity –

 Hiding: Hides the Glyphs.


 Muting: Hiding the glyph makes it vanish completely, on the other hand,
muting the glyph just de-emphasizes the glyph based on the parameters.

Example:
Python3

Output:
Adding Widgets
Bokeh provides GUI features similar to HTML forms like buttons, sliders,
checkboxes, etc. These provide an interactive interface to the plot that allows
changing the parameters of the plot, modifying plot data, etc. Let’s see how to
use and add some commonly used widgets.

 Buttons: This widget adds a simple button widget to the plot. We have to
pass a custom JavaScript function to the CustomJS() method of the models
class.
 CheckboxGroup: Adds a standard check box to the plot. Similarly to
buttons we have to pass the custom JavaScript function to the CustomJS()
method of the models class.
 RadioGroup: Adds a simple radio button and accepts a custom JavaScript
function.

Example:

Python3
Output:

Note: All these buttons will be opened on a new tab.

 Sliders: Adds a slider to the plot. It also needs a custom JavaScript


function.
Example:

Python3

Output:

Similarly, much more widgets are available like a dropdown menu or tabs
widgets can be added.

Note: For complete Bokeh tutorial, refer Python Bokeh tutorial – Interactive
Data Visualization with Bokeh

Plotly
This is the last library of our list and you might be wondering why plotly.
Here’s why –
 Plotly has hover tool capabilities that allow us to detect any outliers or
anomalies in numerous data points.
 It allows more customization.
 It makes the graph visually more attractive.

To install it type the below command in the terminal.


pip install plotly

Scatter Plot

Scatter plot in Plotly can be created using the scatter() method of


[Link]. Like Seaborn, an extra data argument is also required here.
Example:

Python3

Output:

Line Chart
Line plot in Plotly is much accessible and illustrious annexation to plotly which
manage a variety of types of data and assemble easy-to-style statistic.
With [Link] each data position is represented as a vertex

Example:

Python3

Output:
Bar Chart

Bar Chart in Plotly can be created using the bar() method of [Link]
class.

Example:

Python3
Output:

Histogram

In plotly, histograms can be created using the histogram() function of the


[Link] class.

Example:

Python3
Output:

Adding interaction

Just like Bokeh, plotly also provides various interactions. Let’s discuss a few
of them.

Creating Dropdown Menu: A drop-down menu is a part of the menu-button


which is displayed on a screen all the time. Every menu button is associated
with a Menu widget that can display the choices for that menu button when
clicked on it. In plotly, there are 4 possible methods to modify the charts by
using updatemenu method.

 restyle: modify data or data attributes


 relayout: modify layout attributes
 update: modify data and layout attributes
 animate: start or pause an animation

Example:

Python3
Output:
Adding Buttons: In plotly, actions custom Buttons are used to quickly make
actions directly from a record. Custom Buttons can be added to page layouts
in CRM, Marketing, and Custom Apps. There are also 4 possible methods that
can be applied in custom buttons:

 restyle: modify data or data attributes


 relayout: modify layout attributes
 update: modify data and layout attributes
 animate: start or pause an animation

Example:
Python3

Output:
Creating Sliders and Selectors:

In plotly, the range slider is a custom range-type input control. It allows


selecting a value or a range of values between a specified minimum and
maximum range. And the range selector is a tool for selecting ranges to
display within the chart. It provides buttons to select pre-configured ranges in
the chart. It also provides input boxes where the minimum and maximum
dates can be manually input

Example:
Python3

Output:

You might also like