numpy_dataframe
numpy_dataframe
[1 2 3]
[5]: a = np.arange(10,100,10)
print(a)
[10 20 30 40 50 60 70 80 90]
[11. 92.5]
[81]: b = np.random.random(4)
print(b)
[[1 9 3 0]
[6 3 4 0]
[4 9 6 2]]
[22 22 21 20 25 23 27 28 27 25]
1
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[22]: a = (np.zeros((3,3)))
print(a)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[20]: a = np.full((3,4),6)
print(a)
[[6 6 6 6]
[6 6 6 6]
[6 6 6 6]]
[1 2 9 4]
Marks1 to the power of 3 is: [ 1 8 27 64]
[ 5 5 10 8]
Marks1 to the power of 3 is: [ 5 10 15 20]
[18]: np.add(marks1,marks2)
np.subtract(marks1,marks2)
np.multiply(marks1,marks2)
np.divide(marks1,marks2)
np.remainder(marks1,marks2)
2
1.1 Functions of NumPy Arrays
[3]: import numpy as np
arr = np.array([[1,2,3,4],[3,4,5,6]])
print(type(arr))
<class 'numpy.ndarray'>
[14]: print(arr)
[[1 2 3 4]
[3 4 5 6]]
[9]: print(arr.ndim)
print(arr.shape)
print(arr.size)
print(arr.dtype)
2
(2, 4)
8
int32
1.1.1 max()
[15]: print(arr.max())
print(arr.max(axis=0)) # column wise maximum value
print(arr.max(axis=1)) # row wise maximum value
6
[3 4 5 6]
[4 6]
1.1.2 min()
[16]: print(arr.min())
print(arr.min(axis=0)) # column wise minimum value
print(arr.min(axis=1)) # row wise minimum value
1
[1 2 3 4]
[1 3]
[[11 2 13 4]
[ 3 4 5 6]]
3
1.1.3 sum()
Overall sum: 48
Row wise sum: [30 18]
Column wise sum: [14 6 18 10]
1.1.4 sort()
[75]: a = np.array([12, 4, -10, 23, 29, 15, -1, 45, 33, 37, -14])
print(np.sort(a))
[79]: array([ 45, 37, 33, 29, 23, 15, 12, 4, -1, -10, -14])
[ 45 37 33 29 23 15 12 4 -1 -10 -14]
[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]
[[ -9 5 18 9 12]
[ 10 11 3 -5 -10]]
[117]: print(np.sort(b))
[[ -9 5 9 12 18]
[-10 -5 3 10 11]]
4
[[ -9 5 9 12 18]
[-10 -5 3 10 11]]
[[ -9 5 3 -5 -10]
[ 10 11 18 9 12]]
[57]: print(b.flatten())
[ -9 5 18 9 12 10 11 3 -5 -10]
[59]: print(np.sort(b.flatten()))
[-10 -9 -5 3 5 9 10 11 12 18]
[[-10 -9 -5 3 5]
[ 9 10 11 12 18]]
[71]: b
[[ 18 12 11 10 9]
[ 5 3 -5 -9 -10]]
[[ -9 5 18 9 12]
[ 10 11 3 -5 -10]]
[65]: print(np.flip(b))
[[-10 -5 3 11 10]
[ 12 9 18 5 -9]]
[[ 12 9 18 5 -9]
[-10 -5 3 11 10]]
5
[[ 10 11 3 -5 -10]
[ -9 5 18 9 12]]
[143]: emp
0 10
1 20
2 30
dtype: int64
11 Rohan
22 Susan
33 James
dtype: object
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 = pd.Series(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 June
8 August
10 October
12 December
dtype: object
Name Marks
0 Ram 56
1 Rajesh 78
2 Rahul 98
3 Akhil 82
[17]: s1 = s.sort_values(by='Name')
print(s1.head(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(s1.head(5))
Name Marks
0 Ram 56
1 Rajesh 78
2 Rahul 98
3 Akhil 82
Neha 91
Rajeev 93
dtype: int64
/tmp/ipykernel_3999/91782681.py:2: 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 `ser.iloc[pos]`
print(Marks[[2,3]])
[183]: print(Marks.iloc[[2,3]])
Neha 91
Rajeev 93
dtype: int64
Sonali 92
Rajeev 93
dtype: int64
8
Sonali 92
Neha 91
dtype: int64
Sonali 92
Neha 91
Rajeev 93
dtype: int64
Rajeev 93
Neha 91
Sonali 92
Shashi 96
dtype: int64
0 10
1 30
2 50
Name: Roll no, dtype: int32
[17]: s1
[17]: Students
0 10
1 30
2 50
Name: Roll no, dtype: int32
[20]: print(s1.values)
[10 30 50]
[21]: print(s1.size)
9
[22]: print(s1.empty)
False
print(friends.head(2))
11 Rohan
22 Susan
dtype: object
[201]: print(friends.tail())
33 James
44 Riya
55 sumit
66 Abhinav
77 Vihaan
dtype: object
[203]: print(friends.count())
3 DataFrames
[27]: # Creating an empty DataFrame
emp = pd.DataFrame()
print(emp)
Empty DataFrame
Columns: []
Index: []
0
0 10
1 20
2 30
0 1 2
10
0 10 20 30
1 11 22 33
R1 R2 R3
0 10 20 30
1 11 22 33
0
0 Delhi
1 Mumbai
2 Chennai
3 Kolkata
Name Marks
11 Riya 92
22 Sonali 90
33 Sneha 89
Name Marks
11 Riya 92
22 Sonali 90
33 Sneha 89
44 Nithin 84
11
Name Marks Section
11 Riya 92 A
22 Sonali 90 E
33 Sneha 89 E
44 Nithin 84 C
Name Section
11 Riya A
33 Sneha C
44 Nithin C
[ ]:
12