Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python programming U5

BEST AKTU PYTHON NOTES FOR SECOND YEAR

Uploaded by

Harsh Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python programming U5

BEST AKTU PYTHON NOTES FOR SECOND YEAR

Uploaded by

Harsh Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Numpy Important Features & Functions

**Creation of Arrays**: -
`numpy.array`: Create an array from a Python list or tuple. –

`numpy.zeros`: Create an array filled with zeros. –

`numpy.ones`: Create an array filled with ones. –

`numpy.arange`: Create an array with a range of values. –

`numpy.linspace`: Create an array with evenly spaced values.

**Array Manipulation**: -
`numpy.reshape`: Reshape an array. –

- `numpy.sort`: Sort an array.

`numpy.transpose`: Transpose of an array. –

`numpy.concatenate`: Concatenate arrays.

**Mathematical Operations**: -
`numpy.sum`: Sum of array elements. –

`numpy.mean`: Mean of array elements. –

`numpy.median`: Median of array elements. –

`numpy.min` / `numpy.max`: Minimum / maximum value of an array.

`numpy.dot`: Dot product of two array

**Random Number Generation**: -


`numpy.random.rand`: Generate random numbers from a uniform distribution. -
`numpy.random.randn`: Generate random numbers from a standard normal distribution. -
`numpy.random.randint`: Generate random integers. –

`numpy.random.choice`: Generate a random sample from a given 1-D array


plt.pie(y, labels = mylabels, explode = explodedata)
plt.show()

Creating Library file "mylibrary.py"


def f1():
print("A")
print("B")
print("C")
print("D")
def f2(a,b):
x=int(a)
y=int(b)
return x*y

#Using functions of mylibrary.py in another file/program


from mylibrary import *
f1()
x=f2(12,56)
print(x)

#Creating 1 Dimensional Array, with Accessing via loop


import numpy as np
#arr=np.array([12,374,56,6,78,34,89])
n=len(arr)
print("Length is ",n)
m1=min(arr)
m2=max(arr)
print("Minimum is ",m1)
print("Max is ",m2)
for i in range(len(arr)):
print(arr[i])
#Creating 1 Dimensional Array with zeros element
import numpy as np
#arr=np.zeros((10))
#arr=np.arange((10))
arr=np.linspace(0,20,10)
n=len(arr)
print("Length is ",n)
for i in range(len(arr)):
print(arr[i])

#Creating 1 Dimensional Array and calculating sum, mean, and


using where function with sorting
import numpy as np
arr=np.array([12,34,65,45,23,78])
s1=np.sum(arr)
print("Sum of Array Elements:",s1)
s1=np.mean(arr)
print("Mean of Array Elements:",s1)
x=np.where(arr>50)
print(x)
print()
print(arr)
arr.sort()
print(arr)

#Creating 2 Dimensional Array and accessing elements

import numpy as np
arr=np.array(
[
[34,56,23,78],
[23,856,234,23],
[53,45,67,879]
])
for i in range(3):
for j in range(4):
print(arr[i][j],end=" ")
print()
#Creating 2/3 Dimensional array from 1 Dimensional array using
reshape
import numpy as np
#arr=np.zeros((5,4))
arr=np.array([23,45,67,23,56,89,56,23,45,789,56,123])
arr1=arr.reshape(3,4)
print(arr)
print()
print(arr1)
arr2=arr.reshape(4,3)
print(arr2)
print()
arr3=arr.reshape(2,3,2)
print(arr3)
import numpy as np
arr=np.array([23,45,67,23,56,89,56,23,45,789,56,123])
#arr1=np.sqrt(arr)
arr1=np.log(arr)
print(arr)
print(arr1)
Exercise(RRSIMT CLASSES)
1) Create a Program in Python Reverse the Array
without using Library Functions.
2) Create a Program in Python find the Min and Max
Value of an Array without using Library Functions.
3) Create a Program in Python to Reverse the Array.
4) Create a Program in Python to Implement Bubble
Sort on an Array.
5) Create a Program in Python to Implement Selection
Sort on an Array.
6) Create a Program in Python to Implement Merge
Sort on an Array.
7) Create a Program in Python to Transpose the
Matrix. 8) Create a Program in Python to Sum of Two
Matrix.
9) Create a Program in Python to Multiply the Matrix.
10) Create a Program in Python to Generate the OTP
of 6 Digits.
"Sample.csv"
EID,Ename,Post,Department,Salary
1,Rakesh Kumar,Manager,Sales,35000
2,Sanjeev Kumar,Executive,Marketing,21500
3,Amar Singh,Executive,Sales,27000
4,Sumit Bansal,Manager,Marketing,32500
5,Harish Kumar,Executive,Account,22000
6,Akash,Executive,Account,25000
7,Ajeet,Manager,Account,31500
8,Rajesh,Manager,Production,35000
9,Amit Gupta,Executive,Account,19000
10,Atul,Executive,Production,24000

#programs to read csv file to create dataframe and #performing basic


operations
import pandas as pd
df=pd.read_csv("sample.csv")
#print Data Frame
print(df)
#print first 5 rows
print(df.head())
#print first 3 rows
print(df.head(3))
#print last 5 rows
print(df.tail())
#print last 3 rows
print(df.tail(3))
#print information about data frame
print(df.info())
#print means, min, max etc of numerical columns
print(df.describe())

#programs to read csv file to create dataframe and sort salary column
ascending and descending
import pandas as pd
df=pd.read_csv("sample.csv")
df1=df.sort_values(by='Salary')
print(df1)
df1=df.sort_values(by='Salary',ascending=False)
print(df1)

#programs to read csv file to create dataframe and then group by


Department column to get the details like min, max, mean etc.
import pandas as pd
df=pd.read_csv("sample.csv")
gr=df.groupby('Department')
print(gr.max())
print(gr.min())
print(gr.mean())
print(gr.sum())
import pandas as pd
df=pd.read_csv("sample.csv")
df1=df[df['Salary']<20000]
print(df1)

#programs to read csv file to create dataframe and then locate rows.
import pandas as pd
df=pd.read_csv("sample.csv")
#print(df.loc[3])
print(df.loc[5:7])

#creating dataframe from data values


import pandas as pd
data={"name":["Rajat","Maonj","Raghav"],"course":["BCA","B.Tech","MBA"],"age":[16,20,2
2]}
df=pd.DataFrame(data)
print(df)

You might also like