numpy_dataframe
January 10, 2025
1 NumPy ( Numerical Python )
[1]: import numpy as np
roll_no = [Link]([1,2,3])
print(roll_no)
[1 2 3]
[5]: a = [Link](10,100,10)
print(a)
[10 20 30 40 50 60 70 80 90]
[7]: student = [Link]([11, 'Amita', 92.5])
print(student)
['11' 'Amita' '92.5']
[13]: student = [Link]([11, 92.5])
print(student)
[11. 92.5]
[81]: b = [Link](4)
print(b)
[0.45928608 0.60661161 0.85467259 0.39128761]
[91]: b = [Link](10, size=(3,4))
print(b)
[[1 9 3 0]
[6 3 4 0]
[4 9 6 2]]
[111]: print([Link](10, size=10) + 20)
[22 22 21 20 25 23 27 28 27 25]
[21]: a = [Link]((3,4), dtype = int)
print(a)
1
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[22]: a = ([Link]((3,3)))
print(a)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[20]: a = [Link]((3,4),6)
print(a)
[[6 6 6 6]
[6 6 6 6]
[6 6 6 6]]
[27]: a = [Link]([[11, 'Arshia', 93], [15, 'Riddhi', 95]])
print(a)
[['11' 'Arshia' '93']
['15' 'Riddhi' '95']]
[17]: marks1 = [Link]([1,2,3,4])
marks2 = [Link]([2,1,2,1])
print(marks1 ** marks2)
print("Marks1 to the power of 3 is: ", marks1 ** 3)
[1 2 9 4]
Marks1 to the power of 3 is: [ 1 8 27 64]
[11]: marks1 = [Link]([10,20,30,40])
marks2 = [Link]([2,4,3,5])
print(marks1 // marks2)
print("Marks1 to the power of 3 is: ", marks1 // 2)
[ 5 5 10 8]
Marks1 to the power of 3 is: [ 5 10 15 20]
[18]: [Link](marks1,marks2)
[Link](marks1,marks2)
[Link](marks1,marks2)
[Link](marks1,marks2)
[Link](marks1,marks2)
[18]: array([1, 0, 1, 0], dtype=int32)
2
1.1 Functions of NumPy Arrays
[3]: import numpy as np
arr = [Link]([[1,2,3,4],[3,4,5,6]])
print(type(arr))
<class '[Link]'>
[14]: print(arr)
[[1 2 3 4]
[3 4 5 6]]
[9]: print([Link])
print([Link])
print([Link])
print([Link])
2
(2, 4)
8
int32
1.1.1 max()
[15]: print([Link]())
print([Link](axis=0)) # column wise maximum value
print([Link](axis=1)) # row wise maximum value
6
[3 4 5 6]
[4 6]
1.1.2 min()
[16]: print([Link]())
print([Link](axis=0)) # column wise minimum value
print([Link](axis=1)) # row wise minimum value
1
[1 2 3 4]
[1 3]
[18]: arr = [Link]([[11,2,13,4],[3,4,5,6]])
print(arr)
[[11 2 13 4]
[ 3 4 5 6]]
3
1.1.3 sum()
[23]: print("Overall sum: ", [Link]())
print("Row wise sum: ", [Link](axis=1))
print("Column wise sum: ",[Link](axis=0))
Overall sum: 48
Row wise sum: [30 18]
Column wise sum: [14 6 18 10]
1.1.4 sort()
[75]: a = [Link]([12, 4, -10, 23, 29, 15, -1, 45, 33, 37, -14])
print([Link](a))
[-14 -10 -1 4 12 15 23 29 33 37 45]
[79]: # reverse order
[Link]([Link](a))
[79]: array([ 45, 37, 33, 29, 23, 15, 12, 4, -1, -10, -14])
[39]: # reverse order
print([Link](a)[::-1])
[ 45 37 33 29 23 15 12 4 -1 -10 -14]
[43]: # reverse order
r = [Link](-a)
r
[43]: array([-45, -37, -33, -29, -23, -15, -12, -4, 1, 10, 14])
[45]: print(-r)
[ 45 37 33 29 23 15 12 4 -1 -10 -14]
[129]: b = [Link]([[-9, 5, 18, 9, 12],[10, 11, 3, -5, -10]])
print(b)
[[ -9 5 18 9 12]
[ 10 11 3 -5 -10]]
[117]: print([Link](b))
[[ -9 5 9 12 18]
[-10 -5 3 10 11]]
[119]: # Row wise sorting
print([Link](b, axis=1))
4
[[ -9 5 9 12 18]
[-10 -5 3 10 11]]
[55]: # Column wise sorting
print([Link](b, axis=0))
[[ -9 5 3 -5 -10]
[ 10 11 18 9 12]]
[57]: print([Link]())
[ -9 5 18 9 12 10 11 3 -5 -10]
[59]: print([Link]([Link]()))
[-10 -9 -5 3 5 9 10 11 12 18]
[61]: # Overall Sorting
print([Link]([Link]()).reshape([Link]))
[[-10 -9 -5 3 5]
[ 9 10 11 12 18]]
[71]: b
[71]: array([[ -9, 5, 18, 9, 12],
[ 10, 11, 3, -5, -10]])
[73]: # reverse order
print([Link]([Link]([Link]()).reshape([Link])))
[[ 18 12 11 10 9]
[ 5 3 -5 -9 -10]]
1.1.5 reverse order
[63]: print(b)
[[ -9 5 18 9 12]
[ 10 11 3 -5 -10]]
[65]: print([Link](b))
[[-10 -5 3 11 10]
[ 12 9 18 5 -9]]
[67]: print([Link](b, axis=1))
[[ 12 9 18 5 -9]
[-10 -5 3 11 10]]
[69]: print([Link](b, axis=0))
5
[[ 10 11 3 -5 -10]
[ -9 5 18 9 12]]
2 Pandas ( PANel Data )
[3]: # Creating an empty series
import pandas as pd
emp = [Link]()
[143]: emp
[143]: Series([], dtype: object)
[145]: # Creating a series from a NumPy array
data = [Link]([10,20,30])
s1 = [Link](data)
print(s1)
0 10
1 20
2 30
dtype: int64
[147]: # Creating series using labelled index
friends = [Link](['Rohan', 'Susan', 'James'], index=[11,22,33])
print(friends)
11 Rohan
22 Susan
33 James
dtype: object
[149]: # Creating a Series from a Python list
cities = ['Delhi', 'Mumbai','Chennai', 'Kolkata']
s2 = [Link](cities)
print(s2)
0 Delhi
1 Mumbai
2 Chennai
3 Kolkata
dtype: object
[157]: print(s2[0:3])
0 Delhi
1 Mumbai
2 Chennai
dtype: object
6
[159]: # Creating series using labelled index
month = ["June", "August", "October", "December"]
s4 = [Link](month, index=[6,8,10,12])
print(s4)
6 June
8 August
10 October
12 December
dtype: object
[171]: print(s4[[6,12]])
6 June
12 December
dtype: object
[6]: # Creating a series from a python dictionary
month = {6:"June", 8:"August", 10:"October", 12:"December"}
s5 = [Link](month)
print(s5)
6 June
8 August
10 October
12 December
dtype: object
[13]: # File access using Panda
s = pd.read_csv("Student [Link]")
print([Link](5))
# print([Link](10))
# print([Link](5))
Name Marks
0 Ram 56
1 Rajesh 78
2 Rahul 98
3 Akhil 82
[17]: s1 = s.sort_values(by='Name')
print([Link](5))
Name Marks
3 Akhil 82
2 Rahul 98
1 Rajesh 78
0 Ram 56
7
[19]: s1 = s.sort_values(by='Name', ascending=False)
print([Link](5))
Name Marks
0 Ram 56
1 Rajesh 78
2 Rahul 98
3 Akhil 82
[175]: # Accessing the elements of the Series
Marks = [Link]([96, 92, 91, 93], index=["Shashi", "Sonali", "Neha",␣
↪"Rajeev"])
print("Using labelled index: ", Marks["Sonali"])
print("Using Positional index: ", Marks[1])
Using labelled index: 92
Using Positional index: 92
/tmp/ipykernel_3999/[Link]: FutureWarning: Series.__getitem__ treating
keys as positions is deprecated. In a future version, integer keys will always
be treated as labels (consistent with DataFrame behavior). To access a value by
position, use `[Link][pos]`
print("Using Positional index: ", Marks[1])
[177]: # To print 2 elements
print(Marks[[2,3]])
Neha 91
Rajeev 93
dtype: int64
/tmp/ipykernel_3999/[Link]: FutureWarning: Series.__getitem__ treating
keys as positions is deprecated. In a future version, integer keys will always
be treated as labels (consistent with DataFrame behavior). To access a value by
position, use `[Link][pos]`
print(Marks[[2,3]])
[183]: print([Link][[2,3]])
Neha 91
Rajeev 93
dtype: int64
[185]: print(Marks[["Sonali", "Rajeev"]])
Sonali 92
Rajeev 93
dtype: int64
[187]: # Elements can be accessed using Slicing as follows:
print(Marks[1:3]) # excludes the value at index position 3
8
Sonali 92
Neha 91
dtype: int64
[189]: print(Marks["Sonali":"Rajeev"]) # includes the value at labelled index
Sonali 92
Neha 91
Rajeev 93
dtype: int64
[12]: # To display the series in the reverse order
print(Marks[::-1])
Rajeev 93
Neha 91
Sonali 92
Shashi 96
dtype: int64
2.1 Attributes of the Series
[15]: data = [Link]([10, 30, 50])
s1 = [Link](data)
[Link]="Roll no"
print(s1)
0 10
1 30
2 50
Name: Roll no, dtype: int32
[16]: [Link] = "Students"
[17]: s1
[17]: Students
0 10
1 30
2 50
Name: Roll no, dtype: int32
[20]: print([Link])
[10 30 50]
[21]: print([Link])
9
[22]: print([Link])
False
2.2 Built in Functioins in Series
[199]: friends = [Link](["Rohan", "Susan", "James", "Riya", "sumit", "Abhinav",␣
↪"Vihaan"], index=[11, 22, 33, 44, 55, 66, 77])
print([Link](2))
11 Rohan
22 Susan
dtype: object
[201]: print([Link]())
33 James
44 Riya
55 sumit
66 Abhinav
77 Vihaan
dtype: object
[203]: print([Link]())
3 DataFrames
[27]: # Creating an empty DataFrame
emp = [Link]()
print(emp)
Empty DataFrame
Columns: []
Index: []
[28]: # Creating a DataFrame from Numpy Array
a1 = [Link]([10, 20, 30])
a2 = [Link]([11, 22, 33])
f1 = [Link](a1)
f2 = [Link]([a1, a2])
print(f1)
print(f2)
0
0 10
1 20
2 30
0 1 2
10
0 10 20 30
1 11 22 33
[29]: # With column index
a1 = [Link]([10, 20, 30])
a2 = [Link]([11, 22, 33])
f2 = [Link]([a1, a2], columns = ['R1', 'R2', 'R3'])
print(f2)
R1 R2 R3
0 10 20 30
1 11 22 33
[30]: # Creating a DataFrame using a Python List
cities = ['Delhi', 'Mumbai', 'Chennai', 'Kolkata']
f1 = [Link](cities)
print(f1)
0
0 Delhi
1 Mumbai
2 Chennai
3 Kolkata
[39]: # Creating a DataFrame using a Python dictionary
d1 = {'Name':['Riya', 'Sonali', 'Sneha'], 'Marks':[92, 90, 89]}
f2 = [Link](d1, index = [11, 22, 33])
print(f2)
Name Marks
11 Riya 92
22 Sonali 90
33 Sneha 89
3.1 Basic Operations in a DataFrame
[40]: # Adding a new row
[Link][44] = ['Nithin', 84]
print(f2)
Name Marks
11 Riya 92
22 Sonali 90
33 Sneha 89
44 Nithin 84
[41]: # Adding a new column
f2['Section'] = ['A', 'E', 'E', 'C']
print(f2)
11
Name Marks Section
11 Riya 92 A
22 Sonali 90 E
33 Sneha 89 E
44 Nithin 84 C
[45]: # Modifying an existing row
[Link][33] = ['Sneha', 95, 'C']
print(f2)
Name Marks Section
11 Riya 92 A
22 Sonali 97 E
33 Sneha 95 C
44 Nithin 89 C
[44]: # Modifying an existing column
f2['Marks'] = [92, 97, 91, 89]
print(f2)
Name Marks Section
11 Riya 92 A
22 Sonali 97 E
33 Sneha 91 C
44 Nithin 89 C
[50]: # Deleting a row
f2 = [Link](22, axis = 0)
print(f2)
Name Marks Section
11 Riya 92 A
33 Sneha 95 C
44 Nithin 89 C
[51]: # Deleting a column
f2 = [Link]('Marks', axis=1)
print(f2)
Name Section
11 Riya A
33 Sneha C
44 Nithin C
[ ]:
12