Data Analysis with Python - GeeksforGeeks [Link]
org/data-analysis/data-analysis-with-python/
1 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import numpy as np
a = [Link]([2, 2], dtype = int)
print("\nMatrix a : \n", a)
b = [Link](2, dtype = int)
print("Matrix b : \n", b)
Matrix a :
[[ 94655291709206 0]
[3543826506195694713 34181816989462323]]
Matrix b :
[-4611686018427387904 206158462975]
import numpy as np
a = [Link]([2, 2], dtype = int)
2 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
print("\nMatrix a : \n", a)
b = [Link](2, dtype = int)
print("Matrix b : \n", b)
c = [Link]([3, 3])
print("\nMatrix c : \n", c)
Matrix a :
[[0 0]
[0 0]]
Matrix b :
[0 0]
Matrix c :
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
import numpy as np
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
add_ans = a+b
print(add_ans)
add_ans = [Link](a, b)
print(add_ans)
3 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
c = [Link]([1, 2, 3, 4])
add_ans = a+b+c
print(add_ans)
add_ans = [Link](a, b, c)
print(add_ans)
[ 7 77 23 130]
[ 7 77 23 130]
[ 8 79 26 134]
[ 7 77 23 130]
import numpy as np
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
sub_ans = a-b
print(sub_ans)
sub_ans = [Link](a, b)
print(sub_ans)
[ 3 67 3 70]
[ 3 67 3 70]
import numpy as np
4 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
mul_ans = a*b
print(mul_ans)
mul_ans = [Link](a, b)
print(mul_ans)
[ 10 360 130 3000]
[ 10 360 130 3000]
import numpy as np
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
div_ans = a/b
print(div_ans)
div_ans = [Link](a, b)
print(div_ans)
[ 2.5 14.4 1.3 3.33333333]
[ 2.5 14.4 1.3 3.33333333]
5 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import numpy as np
a = [Link](10, 1, -2)
print("\n A sequential array with a negative step: \n",a)
newarr = a[[Link]([3, 1, 2 ])]
print("\n Elements at these indices are:\n",newarr)
A sequential array with a negative step:
[10 8 6 4 2]
Elements at these indices are:
[4 8 6]
•
•
•
6 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import numpy as np
a = [Link](20)
print("\n Array is:\n ",a)
print("\n a[-8:17:1] = ",a[-8:17:1])
print("\n a[10:] = ",a[10:])
Array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
a[-8:17:1] = [12 13 14 15 16]
a[10:] = [10 11 12 13 14 15 16 17 18 19]
import numpy as np
a = [Link](20)
print("\n Array is:\n ",a)
print("\n a[-8:17:1] = ",a[-8:17:1])
print("\n a[10:] = ",a[10:])
Array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
a[-8:17:1] = [12 13 14 15 16]
7 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
a[10:] = [10 11 12 13 14 15 16 17 18 19]
import numpy as np
b = [Link]([[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10, 11, 12]]])
print(b[...,1])
[[ 2 5]
[ 8 11]]
8 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import numpy as np
macros = [Link]([
[0.8, 2.9, 3.9],
[52.4, 23.6, 36.5],
[55.2, 31.7, 23.9],
[14.4, 11, 4.9]
])
cal_per_macro = [Link]([3, 3, 8])
result = macros * cal_per_macro
print(result)
9 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
[[ 2.4 8.7 31.2]
[157.2 70.8 292. ]
[165.6 95.1 191.2]
[ 43.2 33. 39.2]]
import numpy as np
v = [Link]([12, 24, 36])
w = [Link]([45, 55])
print([Link](v, (3, 1)) * w)
X = [Link]([[12, 22, 33], [45, 55, 66]])
print(X + v)
print((X.T + w).T)
10 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
print(X * 2)
[[ 540 660]
[1080 1320]
[1620 1980]]
[[ 24 46 69]
[ 57 79 102]]
[[ 57 67 78]
[100 110 121]]
[[ 24 44 66]
[ 90 110 132]]
•
•
11 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
import numpy as np
ser = [Link](dtype="object")
print(ser)
data = [Link](['g', 'e', 'e', 'k', 's'])
12 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
ser = [Link](data)
print(ser)
Series([], dtype: object)
0 g
1 e
2 e
3 k
4 s
dtype: object
13 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
df = [Link]()
print(df)
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for',
'Geeks']
df = [Link](lst, columns=['Words'])
print(df)
Empty DataFrame
Columns: []
Index: []
Words
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks
14 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
df = pd.read_csv("[Link]")
[Link]()
import pandas as pd
df = pd.read_csv("[Link]")
[Link](["Species", "SepalLengthCm", "SepalLengthCm"]).head()
15 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
df = pd.read_csv("[Link]", header=None)
columns = ["Id", "SepalLengthCm", "SepalWidthCm",
"PetalLengthCm", "PetalWidthCm", "Species"]
[Link] = columns
df_sorted = df.sort_values(by='SepalLengthCm', ascending=True)
print(df_sorted.head())
16 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
17 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
18 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
data1 = {'Name': ['Jai', 'Anuj', 'Jai', 'Princi',
'Gaurav', 'Anuj', 'Princi', 'Abhi'],
'Age': [27, 24, 22, 32,
33, 36, 27, 32],
'Address': ['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj',
'Jaunpur', 'Kanpur', 'Allahabad', 'Aligarh'],
'Qualification': ['Msc', 'MA', 'MCA', 'Phd',
'[Link]', '[Link]', 'Msc', 'MA']}
df = [Link](data1)
print("Original Dataframe")
print(df)
gk = [Link]('Name')
print("After Creating Groups")
[Link]()
19 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
20 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
data1 = {'Name': ['Jai', 'Anuj', 'Jai', 'Princi',
'Gaurav', 'Anuj', 'Princi', 'Abhi'],
'Age': [27, 24, 22, 32,
33, 36, 27, 32],
'Address': ['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj',
'Jaunpur', 'Kanpur', 'Allahabad', 'Aligarh'],
'Qualification': ['Msc', 'MA', 'MCA', 'Phd',
'[Link]', '[Link]', 'Msc', 'MA']}
df = [Link](data1)
grp1 = [Link]('Name')
result = grp1['Age'].aggregate('sum')
print(result)
import pandas as pd
21 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
data1 = {'key': ['K0', 'K1', 'K2', 'K3'],
'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age': [27, 24, 22, 32]}
data2 = {'key': ['K0', 'K1', 'K2', 'K3'],
'Address': ['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'],
'Qualification': ['Btech', 'B.A', 'Bcom', '[Link]']}
df = [Link](data1)
df1 = [Link](data2)
res = [Link]([df, df1], axis=1)
print(res)
22 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
data1 = {'key': ['K0', 'K1', 'K2', 'K3'],
'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],}
data2 = {'key': ['K0', 'K1', 'K2', 'K3'],
'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'],
'Qualification':['Btech', 'B.A', 'Bcom', '[Link]']}
df = [Link](data1)
df1 = [Link](data2)
display(df,df1)
res = [Link](df, df1, on='key')
print(res)
23 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import pandas as pd
data1 = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32]}
data2 = {'Address':['Allahabad', 'Kannuaj', 'Allahabad', 'Kannuaj'],
'Qualification':['MCA', 'Phd', 'Bcom', '[Link]']}
df = [Link](data1,index=['K0', 'K1', 'K2', 'K3'])
df1 = [Link](data2, index=['K0', 'K2', 'K3', 'K4'])
res = [Link](df1)
print(res)
24 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
25 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import [Link] as plt
[Link]([1, 2, 3, 4], [1, 4, 9, 16])
[Link]([0, 6, 0, 20])
[Link]()
import [Link] as plt
import pandas as pd
df = pd.read_csv("[Link]")
[Link](df['Species'], df['SepalLengthCm'])
[Link]("Iris Dataset")
26 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
[Link](["bar"])
[Link]()
import [Link] as plt
import pandas as pd
df = pd.read_csv("[Link]")
[Link](df["SepalLengthCm"])
[Link]("Histogram")
[Link](["SepalLengthCm"])
[Link]()
27 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import [Link] as plt
import pandas as pd
df = pd.read_csv("[Link]")
[Link](df["Species"], df["SepalLengthCm"])
[Link]("Scatter Plot")
[Link](["SepalLengthCm"])
[Link]()
28 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
•
•
•
•
29 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import [Link] as plt
import pandas as pd
df = pd.read_csv("[Link]")
[Link](df["SepalWidthCm"])
[Link]("Box Plot")
[Link](["SepalWidthCm"])
[Link]()
30 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import [Link] as plt
import pandas as pd
df = pd.read_csv("[Link]")
[Link]([Link]() , cmap = 'autumn' , interpolation = 'nearest' )
[Link]("Heat Map")
[Link]()
31 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
•
•
•
•
•
32 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
33 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
34 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import seaborn as sns
import [Link] as plt
35 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
[Link](x='SepalLengthCm', y='SepalWidthCm',
hue='Species', data=df, )
[Link](bbox_to_anchor=(1, 1), loc=2)
[Link]()
•
•
import seaborn as sns
import [Link] as plt
[Link](x='PetalLengthCm', y='PetalWidthCm',
hue='Species', data=df, )
[Link](bbox_to_anchor=(1, 1), loc=2)
[Link]()
36 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
•
•
import seaborn as sns
import [Link] as plt
[Link]([Link](['Id'], axis = 1),
hue='Species', height=2)
37 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
38 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import seaborn as sns
import [Link] as plt
[Link]([Link](method='pearson').drop(
['Id'], axis=1).drop(['Id'], axis=0),
annot = True);
[Link]()
•
•
•
39 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import seaborn as sns
import [Link] as plt
df = pd.read_csv('[Link]')
[Link](x='SepalWidthCm', data=df)
40 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
import sklearn
from [Link] import load_boston
import pandas as pd
import seaborn as sns
df = pd.read_csv('[Link]')
Q1 = [Link](df['SepalWidthCm'], 25,
interpolation = 'midpoint')
Q3 = [Link](df['SepalWidthCm'], 75,
interpolation = 'midpoint')
IQR = Q3 - Q1
print("Old Shape: ", [Link])
upper = [Link](df['SepalWidthCm'] >= (Q3+1.5*IQR))
lower = [Link](df['SepalWidthCm'] <= (Q1-1.5*IQR))
[Link](upper[0], inplace = True)
[Link](lower[0], inplace = True)
print("New Shape: ", [Link])
[Link](x='SepalWidthCm', data=df)
41 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
•
•
42 of 43 12/8/2025, 2:00 PM
Data Analysis with Python - GeeksforGeeks [Link]
43 of 43 12/8/2025, 2:00 PM