[go: up one dir, main page]

0% found this document useful (0 votes)
42 views9 pages

12 IP File Programs 6 To 17

The document discusses various Pandas programs for analyzing student exam data and school results. It includes programs to select rows based on percentage ranges, change percentage values, join dataframes, plot graphs, and import/export CSV data.

Uploaded by

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

12 IP File Programs 6 To 17

The document discusses various Pandas programs for analyzing student exam data and school results. It includes programs to select rows based on percentage ranges, change percentage values, join dataframes, plot graphs, and import/export CSV data.

Uploaded by

abesaale10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SAI SHREE INTERNATIONAL ACADEMY RATLAM

#6 Write a Pandas program to select the rows the percentage is between 70


and 90 (inclusive)
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 whoes percentage more than 70:")
print(df[df['perc'].between(70,90)])

# 7 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)

# 8 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 whoes percentage more than 70:")
print(df[df['perc'] > 70])
#9 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)

#10 Write a Pandas program to append a list of dictioneries or series to a


existing DataFrame and display the combined 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)

s = pd.Series(['Sukhvir', 54,'yes'], index=['name', 'perc','qualify'])

dicts = [{'name': 'Krish', 'perc': 45,'qualify':'yes'},


{'name': 'Kumar', 'perc': 67,'qualify':'yes'}]

print("Original DataFrames:")
print(exam_data1)
print("\nDictionary:")
print(s)
# Add Series
combined_data = exam_data1.append(s, ignore_index=True, sort=False)
# Add Dictionary
combined_info = combined_data.append(dicts, ignore_index=True,
sort=False)
print("\nCombined Data:")
# Print Combined Data/info
print(combined_info)

#11 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 Importing and exporting data between pandas and 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())

#13 Read the ‘Student_result.csv’ to create data frame


import pandas as pd
import csv

#To display Adm_No, Gender and Percentage from ‘student_result.csv’ file.


df = pd.read_csv("student_result.csv",usecols = ['ADM_NO','GENDER',
'PERCENTAGE'])

print("To display Adm_No, Gender and Percentage from ‘student_result.csv’


file.")
print(df)

#To display first 5 and last 5 records from ‘student_result.csv’ file.


df1 = pd.read_csv("student_result.csv")
print(df1.head())
print(df1.tail())

#14 Read the ‘Student_result.csv’ to create data frame and to display


Student_result and to modify the Percentage of student with NaN value in
dataframe.
import pandas as pd
import numpy as np
import csv
df = pd.read_csv("student_result.csv")
print(df)

#To display Student_result file with new column names.


df1 = pd.read_csv("student_result.csv",skiprows = 1,
names = ['Adno','Sex','Name','Eng','Hin',
'Maths','Sc.','SSt','San','IT','Perc'])

print("To display Student_result file with new column names")


print(df1)

# To modify the Percentage of student below 40 with NaN value.


df2 = pd.read_csv("student_result.csv")
print(df2)

print("To modify the Percentage of student below 40 with NaN value.")


df2.loc[(df2['PERCENTAGE'] <40, 'PERCENTAGE')] = np.nan
print(df2)
# Read the ‘Student_result.csv’ to create data frame and to create a duplicate
file for ‘student_result.csv’ containing Adm_No, Name and Percentage.

import pandas as pd
import numpy as np
import csv

#15 To create a duplicate file for ‘student_result.csv’ containing Adm_No,


Name and Percentage.
df = pd.read_csv("student_result.csv")
df.to_csv('copyStudent_result.csv',columns=['ADM_NO',"STUDENT'S_NAME"
,"PERCENTAGE"])
# Display Copied Dataframe
df2=pd.read_csv("copyStudent_result.csv")
print(df2)

# find the highest percentage and also print the student’s name and
percentage.
df1 = pd.read_csv("student_result.csv")
df1 = df1[["STUDENT'S_NAME",'PERCENTAGE']][
df1.PERCENTAGE== df1['PERCENTAGE'].max()]
print(df1)

#16 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 markers 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 = ['Physic','Chemistry','Mathematics', 'Biology','Computer']
marks =[80,75,70,78,60]
plt.plot(subject,marks,'green',marker ='*') # To draw line in red colour
plt.title('Marks Scored') # To Write Title of the Line Chart
plt.xlabel('SUBJECT') # To Put Label At Y Axis
plt.ylabel('MARKS') # To Put Label At X Axis
plt.show()

#17Write 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

**************

You might also like