You are on page 1of 15

CLASS 12 INFORMATICS PRACTICES (065)

PRACTICAL FILE WORK

15 PROGRAMS BASED ON PANDAS

1. Create a pandas series from a dictionary of values and an


ndarray.

# Create a panda’s series from a dictionary of values and a ndarray

import pandas as pd

import numpy as np

s=pd.Series(np.array([1,3,4,7,8,8,9]))

print(s)

# create a dictionary

dictionary = {'X' : 10, 'Y' : 20, 'Z' : 30} # create a series

series = pd.Series(dictionary)

print(series)

2. Write a Pandas program to perform arithmetic operations on


two Pandas Series.
# Write a Pandas program to perform arithmetic operations on two Pandas
Series.

import pandas as pd

ds1 = pd.Series([3, 6, 9, 12, 15])

ds2 = pd.Series([2, 4, 6, 8, 10])


ds = ds1 + ds2

print("Add two Series:")

print(ds)

print("Subtract two Series:")

ds = ds1 - ds2

print(ds)

print("Multiply two Series:")

ds = ds1 * ds2

print(ds)

print("Divide Series1 by Series2:")

ds = ds1 / ds2

print(ds)

3. Write a Pandas program to add some data to an existing


Series.
# Write a Pandas program to add some data to an existing Series.

import pandas as pd

s = pd.Series (['S101', 'Amjad', 'C.Sc.', 'XII – A1', '450'])

print("Original Data Series:")

print(s)

print("\nData Series after adding some data:")

new_s = s.append(pd.Series(['90.0', 'PASS']))


print (new_s)

4. Given a series ,print all the elements that are above the 75th
percentile

# Create a panda’s series from a dictionary of values and a ndarray

import pandas as pd

import numpy as np

s=pd.Series(np.array([1,3,4,7,8,8,9]))

print(s)

res=s.quantile(q=0.75)

print()

print('75th Percentile of the series is:::')

print(res)

print()

print('The elements that are above the 75th percentile ::')

print(s[s>res])

5. Write a Pandas program to select the rows where the


percentage greater than 70

# Write a Pandas program to select the rows where the percentage greater than 70.

import pandas as pd
import numpy as np
exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',
'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_data , index=labels)
print ("Number of student whose percentage more than 70:")
print (df[df['perc'] > 70])

6. Write a Pandas program to change the percentage in a given


row by the user.

# Write a Pandas program to change the percentage in given row by user.


import pandas as pd
import numpy as np

exam_dic = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',


'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_dic , index=labels)
print("\nOriginal data frame:")
print(df)
ch = input("Enter the index of row : ")
per = float(input("Enter percentage to be changed: "))
print('\nChange the percentage in row '+ch+ ' to', per)
df.loc[ch, 'perc'] = per
print(df)

7. Write a Pandas program to join the two given dataframes


along rows and assign all data.

# Write a Pandas program to join the two given dataframes along rows and assign
all data.

import pandas as pd
import numpy as np

exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',


'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}

exam_data1 = pd.DataFrame(exam_dic1)

exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],


'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}

exam_data2 = pd.DataFrame(exam_dic2)

print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)
8. Write a Pandas program to join the two given dataframes
along columns and assign all data.
# Write a Pandas program to join the two given dataframes along columns and
assign all data.

import pandas as pd
import numpy as np

exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',


'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}

exam_data1 = pd.DataFrame(exam_dic1)

exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],


'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}

exam_data2 = pd.DataFrame(exam_dic2)

print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2],axis=1)
print(result_data)
9. Create a dataframe quarterly sales where each row contains the
item category ,item name and expenditure. Group the rows by
category and print the total expenditure per category.

import pandas as pd

QtrSales = pd.DataFrame ({
"Item Category":['A', 'B', 'A', 'A', 'B', 'C', 'B', 'C'],
'Item Name': ['iPad', 'LCD', 'iPhone', 'iWatch', 'Projector', 'Hard disk', 'Smartboard',
'Pen drive'],
'Expenditure': [288000, 356000, 497000, 315000, 413000, 45000, 211000, 21000]})

print("Dataframe QtrSales is:")


print (QtrSales)
print(" 3 largest expenditure values in given dataframe are:" )
print(QtrSales.sort_values ("Expenditure", ascending = False).head(3))

10. Write a python program to create a DataFrame for examination result and
display row labels, column labels data types of each column and the dimensions.

import pandas as pd
df=pd.DataFrame()
df['Name']=['A','B','C','D']
df['Class']=[12,10,11,12]
df['Mrks1']=[88,85,89,90]
df['Mrks2']=[55,67,97,87]
df['Mrks3']=[77,75,74,73]
print(df)
print('\n')
print(df.shape)
11. Program to select or filter rows from a DataFrame based
on values in columns in pandas.( Use of Relational and Logical
Operators)

# Program to select or filter rows from a DataFrame based on values in columns in


pandas.( Use of Relational and Logical Operators)

import pandas as pd

import numpy as np

exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit', 'Sumit', 'Matthew',


'Kartik', 'Kavita', 'Pooja'],

'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],

'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}

exam_data1 = pd.DataFrame(exam_dic1)

print("Original DataFrames:")

print(exam_data1)

print("\nUse == operator\n")

print(exam_data1.loc[exam_data1['name'] == 'Rohan'])
print("\nUse < operator\n")

print(exam_data1.loc[exam_data1['perc'] < 40])

print("\n Use != operator\n")

print(exam_data1.loc[exam_data1['qualify'] != 'no'])

print("\n Multiple Conditions\n")

print(exam_data1.loc[(exam_data1['qualify'] != 'yes') & (exam_data1['perc'] <40)])

12. Filter out rows based on different criteria such as duplicate


rows

# Filter out rows based on different criteria such as duplicate rows

import pandas as pd

data={'Name':['Aman','Rohit','Deepika','Aman','Deepika','Sohit','Geeta'],

'Sales':[8500,4500,9200,8500,9200,9600,8400]}

sales=pd.DataFrame(data)

# Find duplicate rows


duplicated = sales[sales.duplicated(keep=False)]

print("duplicate Row:\n",duplicated)

12. Importing and exporting data between pandas and csv file

# importing data from csv file

import pandas as pd

df=pd.read_csv ('f:\student.csv')

print(df)

# exporting (writing ) data to csv file

import pandas as pd
l=[{'name':'Rocky','surname':'Dsouza'}]
df= pd.DataFrame(l)
df.to_csv ('f:\student.csv')
print(df)

13. To create a dataframe from csv file and display


row,column labels ,dimension and shape.

# To create and open a data frame using ‘Student_result.csv’


file using Pandas.
# To display row labels, column labels data types of each
column and the dimensions
# To display the shape (number of rows and columns) of the
CSV file.
import pandas as pd

import csv

#Reading the Data

df = pd.read_csv("student_result.csv")

# Display Name of Columns

print(df.columns)

# Display no of rows and column

print(df.shape)

# Display Column Names and their types

print(df.info())

14. Use of Head function in Dataframe

import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],
'age': [25, 30, 35, 40, 45]
})

# use the head() function to retrieve the first 3 rows of the DataFrame
print(df.head(3))
15. Use of tail function in dataframe

import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],
'age': [25, 30, 35, 40, 45]
})

# use the tail() function to retrieve the last 2 rows of the DataFrame
print(df.tail(2))

CLASS 12 INFORMATICS PRACTICES (065)

PRACTICAL FILE WORK

04 ROGRAMS BASED ON MATPLOTLIB

01.Given the school result data, analyses the performance of


the students on different parameters, e.g subject wise or
class wise.

# Given the school result data, analyses the performance of the students on
#different parameters, e.g subject wise or class wise.

# x-axis is shows the subject and y -axis

# shows the marks in each subject


# import pandas and matplotlib

import pandas as pd

import matplotlib.pyplot as plt

# Simple Line Chart with setting of Label of X and Y axis,

# title for chart line and color of line

subject = ['Physics','Chemistry','Mathematics', 'Biology','Computer']

marks =[80,75,70,78,82]

# To draw line in red colour

plt.plot(subject,marks,'r',marker ='*')

# To Write Title of the Line Chart

plt.title('Marks Scored')

# To Put Label At Y Axis

plt.xlabel('SUBJECT')

# To Put Label At X Axis

plt.ylabel('MARKS')

plt.show()
02. Write a program to plot a bar chart in python to
display the result of a school for five consecutive years.

#Write a program to plot a bar chart in python to display the result of a school
for five consecutive years.

import matplotlib.pyplot as pl

year=['2015','2016','2017','2018','2019'] # list of years

p=[98.50,70.25,55.20,90.5,61.50] #list of pass percentage

j=['b','g','r','m','c'] # color code of bar charts

pl.bar(year, p, width=0.2, color=j) # bar( ) function to create the bar chart

pl.xlabel("year") # label for x-axis

pl.ylabel("Pass%") # label for y-axis

pl.show( ) # function to display bar chart

03. Write a Python program to plot several lines with


different format styles in one command using arrays.

import numpy as np
import matplotlib.pyplot as plt

# Sampled time at 200ms intervals


t = np.arange(0., 5., 0.2)

# green dashes, blue squares and red triangles


plt.plot(t, t, 'g--', t, t**2, 'bs', t, t**3, 'r^')
plt.show()

04. Write1. Write a Python programming to display a bar


chart of the popularity of programming Languages.
Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#,
C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

import matplotlib.pyplot as plt


x = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']
popularity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, popularity, color='blue')
plt.xlabel("Languages")
plt.ylabel("Popularity")
plt.title("Popularity of Programming Language\n" + "Worldwide, Oct 2017
compared to a year ago")
plt.xticks(x_pos, x)
# Turn on the grid
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
# Customize the minor grid
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()

________________________________________________________________

You might also like