You are on page 1of 6

#Numpy

#Calculating percentiles
import numpy as np
arr = np.array([71, 92, 82, 97, 74, 68])
print("Data (arr):\n",arr,"\n")# Calculating 20th
percentile
res = np.percentile(arr, 20)
print("20th Percentile is:\n",res)
res = np.percentile(arr, 85)# Calculating 85th percentile
print("85th Percentile is:\n",res)
res = np.percentile(arr, 21)# Calculating 21st percentile
print("21st Percentile is:\n",res)

# Calculating mean in an array


import numpy as np
inputArr = [12, 17, 25, 87, 23]
print("inputArray : ", inputArr)
print("Mean of inputArray : ", np.mean(inputArr))#
calculate mean of inputArray
#check if numpy array is multidimensional or not
import numpy as np
array = np.array([[[0,8,9,4],[5,6,7,2],[4,8,2,5]],[[7,9,7,0],
[6,7,2,8],[8,7,6,5]]])
print("array:\n",array,"\n")
res = array.ndim
print("Result:\n",res,"\n")

#Pandas

#Groupping DataFrame
import pandas as pd
dict = {'Name':
['Himadri','Shiv','Krishna','Rajat','Manav','Aryan','Arjun',
'Ram','Aditya','Gaurav',],
'Subjects':['Painting','Hindi','Computer
Science','Informatics Practices','Bio-
technology','Geography',
'Philosophy','Sociology','Computer Science','Informatics
Practices']}
df = pd.DataFrame(dict)
print("Subjects selected by students:\n",df,"\n")
result = df.groupby('Subjects')['Name'].apply(list)#
Grouping the column
print("Grouped values, people with particular sports:\
n",result)

# Determining whether Column contains a particular


value
import pandas as pd
d = {'Name':['Himadri', 'Shiv', 'Krishna', 'Manav',
'Rajat'],}
df = pd.DataFrame(d,index = ['a', 'b', 'c', 'd', 'e',])
print("Created Dataframe:\n", df)# check 'Jyoti' exist in
DataFrame or not
if 'Himadri' in df.values :
print("\nYes,'Himadri' is in DataFrame")
#Replacing all values in a column
import pandas as pd
d = {"Students":
['Himadri','Shiv','Krishna','Rajat','Manav'],
"Class":
['Class_11','Class_11','Class_11','Class_11','Class_11'],
"Roll_no":[7,9,17,15,21]}
df = pd.DataFrame(d)
print("DataFrame:\n",df,"\n\n")
df.loc[(df.Class == 'Class_11' ), 'Class'] = 'Class_12'#
Replacing thr values of column
print("Modified DataFrame:\n",df)

#Getting a list of all the duplicate items


import pandas as pd
df = pd.DataFrame(data = {'Books':['Harry Potter' ,'The
Lord of the Rings','The Hobbit',
'Assassins Apprentice','The Night Circus','An Ember in
the Ashes','Harry Potter','The Magic Tree' ,
'A Spell Too Far','The Enchanted Ones','THE LOST
HERO','A Spell Too Far','The Lord of the Rings']})
print("Original Books:\n",df,"\n")
result = df[df.duplicated(['Books'], keep='first')]#
Getting the duplicates from parle column
print("Duplicates Books:\n",result)

#Matplotlib

#Making a Bar graph


import matplotlib.pyplot as plt
Class= ['12', '11', '10', '9']
Students= [70,81,64,89]
plt.bar(Class, Students)
plt.title('Students in diff classes')
plt.xlabel('Classes')
plt.ylabel('Students')
plt.show()

#Scatter plot
import matplotlib.pyplot as plt
height = [197, 182, 165, 191, 127, 171, 181, 184, 183,
168]
weight = [70, 82, 59, 91, 81, 71, 101, 71, 62, 68]
plt.scatter(weight, height)
plt.show()

You might also like