You are on page 1of 21

Data handling (pandas) – 25 marks

Series , datafarame , pyplot


MySQL xi and xii – 25 marks
(functions , group by clause , joining)
Networking – 10 marks
Full forms
Short case study (5 marks)
Social impact-10 marks
Short questions)

Practical's
Exam – pandas + mysql = 15 marks
Practical file – 5marks
Project – 5 marks
Viva – 5 marks
DATA
HANDLING
IN PANDAS
(25 marks)
session 2020-21
INTRODUCTION TO PANDAS
• Python is an open source Python library providing high
performance data manipulation and analysis tool using
its powerful data structures like series and data frames.
• Pandas stand for PANEL DATA.
• it provides fast data processing as number along with
data manipulation techniques as spreadsheets and
relational databases.
PANDAS DATA TYPES
• Object - it is used for string data type values

• Int64 - it is used for integers that is the numbers without decimals.

• float 64 - it is used for float values that is the real numbers .

• bool - it is used for Boolean values that is true or false.


PANDAS- SERIES
Series is the primary building block of Pandas. It is a one dimensional labelled
array capable of holding data of any type. The data in a series is mutable but the
number of members in a series is immutable.

A series in pandas can be created using series() method. Also any list / dictionary can be converted
In to series using this method.

Creating an empty series


import pandas as pd Series([], dtype: float64)
S=pd.Series()
print(S)

Creating an integer series using a list

import pandas as pd
S=pd.Series([10,12,14,16])
print(S)

Creating a series with the different values


Series() Method
• Creates a series From a scalar value, list, array or dictionary.
• it has a parameter called index, which is optional if not given the
series items are indexed with the value of 0 to n-1 for a base
object with n elements.

Creating series using scalar data types


import pandas as pd
S=pd.Series(7,index=[0,1,2,3,4])
print()
print(S)

Creating a series with the value 7 in all


CREATING SERIES USING LISTS
import pandas as pd
months = ['jan','feb','mar','apr','may']
l=[1,3,6,78,99,78]
S1=pd.Series(months)
S2=pd.Series(l)
print(S1)
print(S2)
CREATING SERIES USING ARRAYS: numpy
import pandas as pd
import numpy as np
l=[1,2,4,8,98]
arr=np.array(l)
S=pd.Series(arr) #without index parameter
print ("Array is:",arr)
print("Series is:")
print(S)
CREATING A SERIES FROM DICTIONARY
What is a dictionary-Dictionaries in Python are list of key value pairs.
Example- dict = {'A' : 10, 'B' : 20, 'C' : 30}

# Code to create series using dictionaries without index parameter


INDEX
import pandas as pd

dict = {'A' : 10, 'B' : 20, 'C' : 30}

S=pd.Series (dict)

print(S)

KEYS of the DICTIONARY will become index for the SERIES


CREATING A SERIES FROM DICTIONARY

# Creating Series with index parameter


import pandas as pd

dict = {'A' : 10, 'B' : 20, 'C' : 30}

S=pd.Series(dict, index=['B','C', 'A']) INDEX

print()

print(S)
'''WAP TO OBTAIN 5 NUMBER FROM THE
USER IN A LIST AND TEN CREATE A SEIRES
FROM IT AND THEN DISPLAY THE SUM AND
AVERAGE OF ALL THE SERIES ELEMENTS.'''

import pandas as pd
ls=[]
for i in range(1,6):
n=int(input("enter elements "))
ls.append(n)
print("list ")
print(ls)
sr1=pd.Series(ls)
print("SERIES ")
print(sr1)
l=len(sr1)
s=0
av=0
for i in range(0,l):
s=s+sr1[i]
av=s/l
print("sum of seires ",s)
print("average of series ",av)
ACCESSING SERIES INDEX AND VALUES
Index is used to display index values
Values shows the values in a series.

#Code to show the use of index and values


import pandas as pd
dict = {'A' : 10, 'B' : 20, 'C' : 30}
S=pd.Series (dict)
print()
print(S)
print(S.index)
print(S.values)
Accessing rows using head() and tail() functions
head () function returns first 5 elements of the series
tail() function returns last 5 elements of the list by default
import pandas as pd
Months=['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
S1=pd.Series(Months)
print(S1.head())
print (S1.tail())
Accessing rows using head() and tail() functions

import pandas as pd
Months=['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
S1=pd.Series(Months)
print(S1.head(2))
print (S1.tail(3))
SERIES ATTRIBUTES
print("data type ",sr1.dtype)#data type of the seires import pandas as pd
l=[300,150,450,600,250,800,1000]
print("shape ",sr1.shape)#shape of the series in form tuple
sr1=pd.Series(l)
print("dimension ",sr1.ndim)#number of dimension
print(sr1)
print("size ",sr1.size)#number of elements in the series
print("index ",sr1.index)#show the axis label
print("values ",sr1.values)#display values of the seires
print("no of bytes ",sr1.nbytes)#returns number of bytes (int64 and float64= 8 bytes)
print("hasnans ",sr1.hasnans)#gives true if series has any NaN value
print("empty ",sr1.empty)#gives ture is series is empty
print("item size ",sr1[1].itemsize)#return size of each item'''
data type int64
shape (7,)
dimension 1
size 7
index RangeIndex(start=0, stop=7, step=1)
values [ 300 150 450 600 250 800 1000]
no of bytes 56
hasnans False
empty False
item size 8

You might also like