Pandasmatplotlib Practical File
Pandasmatplotlib Practical File
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
series = pd.Series(dictionary)
print(series)
import pandas as pd
print(ds)
ds = ds1 - ds2
print(ds)
ds = ds1 * ds2
print(ds)
ds = ds1 / ds2
print(ds)
import pandas as pd
print(s)
4. Given a series ,print all the elements that are above the 75th
percentile
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(res)
print()
print(s[s>res])
# 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])
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)
# 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_data1 = pd.DataFrame(exam_dic1)
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_data1 = pd.DataFrame(exam_dic1)
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]})
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)
import pandas as pd
import numpy as np
'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['qualify'] != 'no'])
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)
print("duplicate Row:\n",duplicated)
12. Importing and exporting data between pandas and csv file
import pandas as pd
df=pd.read_csv ('f:\student.csv')
print(df)
import pandas as pd
l=[{'name':'Rocky','surname':'Dsouza'}]
df= pd.DataFrame(l)
df.to_csv ('f:\student.csv')
print(df)
import csv
df = pd.read_csv("student_result.csv")
print(df.columns)
print(df.shape)
print(df.info())
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))
# Given the school result data, analyses the performance of the students on
#different parameters, e.g subject wise or class wise.
import pandas as pd
marks =[80,75,70,78,82]
plt.plot(subject,marks,'r',marker ='*')
plt.title('Marks Scored')
plt.xlabel('SUBJECT')
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
import numpy as np
import matplotlib.pyplot as plt
________________________________________________________________