Pandas
Pandas is a Python library.
Pandas is used to analyze data.
What is Pandas?
• Pandas is a Python library used for working with data
sets.
• It has functions for analyzing, cleaning, exploring, and
manipulating data.
• The name "Pandas" has a reference to both "Panel
Data", and "Python Data Analysis" and was created by
Wes McKinney in 2008.
Why Use Pandas?
• Pandas allows us to analyze big data and make
conclusions based on statistical theories.
• Pandas can clean messy data sets and make them
readable and relevant.
• Relevant data is very important in data science.
Installation of Pandas
• pip install pandas
Import Pandas
Once Pandas is installed, import it in your applications by adding the import keyword:
import pandas
Or
import pandas as pd
Checking Pandas Version
print(pd.__version__)
Pandas as pandas Series
• Syntax
• Pandas_series=[Link](data=None, index=None, dtype=None, nam
e=None, copy=None, fastpath=<no_default>)
• # Person names and their income
• person_names = ['Alice', 'Bob', 'Charlie’]
• income_list = [50000, 60000, 70000]
# Person names and their income using tuple
income_tuple = (75000, 85000, 95000)
person_index = ('David', 'Eve', 'Frank’)
# calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = [Link](calories)
What is a DataFrame?
• A Pandas DataFrame is a 2 dimensional data structure,
like a 2 dimensional array, or a table with rows and
columns.
Example
• import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
#load data into a DataFrame object:
df = [Link](data)
print(df)
Locate Row
Pandas use the loc attribute to return one or more specified row(s)
• [Link][0]
Execise
Try to get
• Return row 0 and 1:
Try With Different Index name to access rows
Try
import pandas as pd
# Data with two lists
data = [ [420, 50], [380, 40], [390, 45] ]
# Specifying custom column names
df = [Link](data,)
print(df)
Try this using columns
import pandas as pd
# Data with two lists
data = [ [420, 50], [380, 40], [390, 45] ]
# Specifying custom column names
df = [Link](data, columns=['Calories',
'Duration’])
print(df)
Adding dataframe’s
import pandas as pd
col=['name','class','rollno']
data1=[{'name':'Manoj','class':'BCA','rollno':4}]
df1=[Link](data=data1,columns=col)
data2=[{'name':'Ramesh','class':'BCA','rollno':24}]
df2=[Link](data=data2,columns=col)
df=[Link]([df1,df2],ignore_index=True)
print(df)
Reading CSV File
import pandas as pd
df = pd.read_csv('[Link]')
Viewing the Data
One of the most used method for getting a quick overview of the DataFrame, is the head() method.
The head() method returns the headers and a specified number of rows, starting from the top.
import pandas as pd
df = pd.read_csv('[Link]')
print([Link](10))
There is also a tail() method for viewing the last rows of the DataFrame.
print([Link]())
The DataFrames object has a method called info(), that gives you more information about the data set.
print([Link]())
Pandas - Cleaning Data
• Data cleaning means fixing bad data in your data set.
• Bad data could be:
• Empty cells
• Data in wrong format
• Wrong data
• Duplicates
Pandas - Cleaning Empty Cells
• Empty Cells
• Empty cells can potentially give you a wrong result
when you analyze data.
Remove Rows
• One way to deal with empty cells is to remove rows that contain empty cells.
• print([Link]())
• import pandas as pd
df = pd.read_csv('[Link]')
new_df = [Link]()
• use the inplace = True argumentthe dropna(inplace = True) will NOT return a new
DataFrame, but it will remove all rows containing NULL values from the original
DataFrame.
• import pandas as pd
df = pd.read_csv('[Link]')
[Link](inplace = True)
the dropna(inplace = True) will NOT return a new DataFrame, but it will remove
all rows containing NULL values from the original DataFrame.
DEAL WITH MISSING VALUES
Missing Value Replace with -Zero
• [Link](0)
Missing Value Replace with –any constant value
• [Link](value=values_to_fill)
Missing Value Replace with –Interpolation
• [Link](method='linear')
DEAL WITH MISSING VALUES
Missing Value Replace with -Mean
• [Link]([Link]())
Missing Value Replace with –upper
value
• [Link](method='ffill’) # Forward Filling
Missing Value Replace with –below
value
• [Link](method='bfill’) # Backward Filling