Professional Documents
Culture Documents
m1=[[11,12,13],
[14,15,16],
[17,18,19]]
m2=[[1,2,3],
[4,5,6],
[7,8,9]]
add=[[0,0,0],
[0,0,0],
[0,0,0]]
sub=[[0,0,0],
[0,0,0],
[0,0,0]]
mul=[[0,0,0],
[0,0,0],
[0,0,0]]
print('Addition :- ')
for i in range(len(m1)):
for j in range(len(m1[0])):
add[i][j] = m1[i][j] + m2[i][j]
print(add)
print('Subtraction :- ')
for i in range(len(m1)):
for j in range(len(m1[0])):
sub[i][j] = m1[i][j] - m2[i][j]
print(sub)
print('Multiplication :- ')
for i in range(len(m1)):
for j in range(len(m2[0])):
for k in range(len(m2)):
mul[i][j] = m1[i][k] - m2[k][j]
print(mul)
Que 3 Create two pandas series from a dictionary of values and an ndarray and display the values from
2nd to 5th index. Print the index, minimum and maximum values in the first series.
import pandas as pd
import numpy as np
dict1 = {'Devaansh': 100,'Harshil': 200,'Pryank': 300,'Raghav': 400,'Keshav': 500,'Madhav':600}
arr1 = np.array([300,400,500, 600, 700, 800])
Seriesdict1 = pd.Series(dict1)
Seriesarr1 = pd.Series(arr1)
print("Series from Dictionary:")
print(Seriesdict1[2:6])
print("\nSeries from ndarray:")
print(Seriesarr1[2:6])
print(Seriesdict1.index)
print(Seriesdict1.min())
print(Seriesdict1.max())
Que 4 Create a Series and print all the elements that are above 75th percentile. Print
minimum ,maximum and sum of series using aggregate()
import pandas as pd
import numpy as np
list1 = [10, 25, 55, 75, 90, 115, 135]
Se = pd.Series(list1)
print(Se)
print("Series Above 75 Percentile \n")
PERCNT = np.percentile(Se, 75)
Series75 = Se[Se > PERCNT]
print(Series75)
Que 5 Series objects Temp1, temp2, temp3, temp 4 stores the temperature of
days of week 1, week 2, week 3, week 4. Write a script to:-
a. Print average temperature per week
b. Print average temperature of entire month
import pandas as pd
avgtem = [28,31,26,28,31,34,37,
28,33,25.0,35,35.4,29,27,
24,30,29,39,37,39,40,
27,36,34,30,22,25,27,
28,30,25]
T1 = pd.Series(avgtem[:7])
T2 = pd.Series(avgtem[8:15])
T3 = pd.Series(avgtem[16:23])
T4 = pd.Series(avgtem[23:30])
#A
print("---------AVERAGE TEMPERATURE OF WEEKS---------\n\n")
print('Average Temp of Week 1 : ', T1.sum()/7, "\n")
print('Average Temp of Week 2 : ', T2.sum()/7, "\n")
print('Average Temp of Week 3 : ', T3.sum()/7, "\n")
print('Average Temp of Week 4 : ', T4.sum()/7, "\n")
print("\n")
#B
Sum = T1.sum() + T2.sum() + T3.sum() + T4.sum()
print("---------AVERAGE TEMPERATURE OF MONTH---------\n\n")
print('Average temp of entire month : ', Sum/len(avgtem))
Que 6 Create a series containing 2 NaN values. Perform check for null values and then replace the null
values with 0.perform any 5 statistical functions on the series.
import pandas as pd
import numpy as np
null_check = s1.isnull()
print("Null values check:\n", null_check)
s1 = s1.fillna(0)
print("\nSeries with null values replaced:\n", s1)
print("\nStatistical functions:")
print("Mean:", s1.mean())
print("Median:", s1.median())
print("Standard Deviation:", s1.std())
print("Minimum:", s1.min())
print("Maximum:", s1.max())
Que 7 Two Series object, Population stores the details of four metro cities of India and another object AvgIncome
store the total average income reported in four years in these cities. Calculate income per capita for each
of these metro cities.
import pandas as pd
Population = {'Delhi': 20000000, 'Mumbai': 22000000, 'Kolkata':
15000000, 'Chennai': 10000000}
Series_Population = pd.Series(Population)
Income = {'Delhi': 1000000, 'Mumbai': 1200000, 'Kolkata': 900000,
'Chennai': 800000}
Series_Income = pd.Series(Income)
Per_Capita = Series_Income / Series_Population
print("-----Income per capita:-----\n")
print(Per_Capita)
Que 8 Given two series S1 and S2.
S1 S2
A 39 A 10
B 41 B 10
C 42 D 10
D 44 F 10
Find the output for following python pandas statements?
a. S1[ : 2]*100
b. S1 * S2
c. S2[ : : -1]*10
import pandas as pd
import pandas as pd
import numpy as np
series = pd.Series(rn)
print(series)
Que 10 Consider a series object s10 that stores the number of students in each section of class 12 as
shown below. First two sections have been given task for selling tickets @ Rs.100/- per ticket as a part of
social experiment. Write code to create the series and display how much section A and B have collected.
A-39, B- 31, C- 32, D- 34, E- 35
import pandas as pd
s10 = pd.Series([39, 31, 32, 34, 35], index=['A', 'B', 'C', 'D', 'E'])
import pandas as pd
df = pd.DataFrame(data)
print("DataFrame:")
print(df)
print("\nTranspose of DataFrame:")
print(df.transpose())
df = df.append(pd.DataFrame(new_data), ignore_index=True)
print(df.index.tolist())
print(df.columns.tolist())
df_sorted = df.sort_values('Age')
print(df_sorted)
Que 12 Create a DataFrame having age, name, weight of five students. Print the dataframe using head().
Modify the weight of student in first and 4th row. Display only the weight of first and fourth rows before
and after modification.
import pandas as pd
student_data = {
df = pd.DataFrame(student_data)
print(selected_weights)
Que 13 Create a DataFrame based on E-Commerce data and generate mean, mode, median.
import pandas as pd
DF = pd.DataFrame(DICT1)
Me = DF['Marks'].mean()
Mo = DF['Marks'].mode().values[0]
Med = DF['Marks'].median()
print("-----------Statistical Measures-----------\n")
drive.mount('/content/drive')
import pandas as pd
df = pd.read_csv("/content/drive/MyDrive/Book1.csv")
print(df)
print(df.describe())
Que 15 Consider the DataFrame QtrSales where each row contains the item category, item name and
expenditure and group the rows by category, and print the average expenditure per category
import pandas as pd
QtrSales = pd.DataFrame({
'Clothing', 'Grocery'],
'Snacks'],
average_expenditure =QtrSales.groupby('Category')['Expenditure'].mean()
print(average_expenditure)
Que 16 Write a program to implement pivot() and pivot-table() on a DataFrame
import pandas as pd
print(df)
import pandas as pd
df = pd.DataFrame({"ram":[25,27,26,28,29],
"krishna":[23,20,24,26,22],
"kahna":[28,26,29,31,36],
"radha":[31,35,29,35,30]})
print(df)
print(df.mad(axis = 0))
Que 18 Create a DataFrame based on employee data and generate quartile and variance.
import pandas as pd
import numpy
emp=pd.DataFrame({"Name":['ram','krishna','kahna','radha','rani'],
"Age":[24,26,28,30,32],
"Salary":[2400000,2800000,3200000,3600000,2400000],
"Experience":['3Y','5Y','7Y','6Y','8Y']})
print(emp)
print(emp['Salary'].var())
Que 19 Program to implement Skewness on Random data.
x = [55, 78, 65, 98, 97, 60, 67, 65, 83, 65]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 24.1
print(df)
# 24.2
print(df.describe())
# 24.3
df_renamed = df.rename(columns={'id': 'anime_ID', 'member': 'men'})
print(df_renamed)
# 24.4
subset = df.iloc[2:5, 1:4]
print(subset)
# 24.5
# Convert non-numeric values in 'episodes' column to NaN
df['episodes'] = pd.to_numeric(df['episodes'], errors='coerce')