0% found this document useful (0 votes)
55 views11 pages

Data Visualization Techniques in Python

The document is a Jupyter notebook focused on data visualization and manipulation using Python libraries such as NumPy and Pandas. It includes examples of data wrangling, imputation, reshaping data frames, and filtering data with various operations and techniques. The notebook demonstrates how to create and modify data frames, handle missing values, and perform pivot operations.

Uploaded by

bhumikabhavre23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views11 pages

Data Visualization Techniques in Python

The document is a Jupyter notebook focused on data visualization and manipulation using Python libraries such as NumPy and Pandas. It includes examples of data wrangling, imputation, reshaping data frames, and filtering data with various operations and techniques. The notebook demonstrates how to create and modify data frames, handle missing values, and perform pivot operations.

Uploaded by

bhumikabhavre23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/31/25, 11:12 AM DV Unit-4.

ipynb - Colab

Data Visualization by Mangesh Balpande

keyboard_arrow_down Data Wrangling

import numpy as np
import pandas as pd
d1= {
"A": [Link](1,50,10),
"B": [Link](1,50,10),
"C": [Link](1,50,10),
"D": [Link](1,50,10),
}
df = [Link](d1)
df

A B C D

0 35 22 1 47

1 15 17 17 30

2 39 5 19 28

3 30 10 9 22

4 8 44 45 33

5 4 45 11 23

6 33 38 3 24

7 34 27 25 3

8 23 38 45 33

9 15 38 17 27

arr = [Link](1,50,(10,4))
df1= [Link](arr, columns= list("ABCD"))
df1

A B C D

0 13 35 34 22

1 41 24 39 30

2 11 10 24 10

3 35 14 10 41

4 16 26 1 14

5 46 35 35 28

6 19 35 17 46

7 20 34 34 32

8 8 24 41 8

9 30 8 18 7

import numpy as np
import pandas as pd
d1 = {
"roll":[i for i in range(1,11)],
"CNIP": [Link](1,56,10),
"SE": [Link](1,56,10),
"DV": [Link](1,56,10)
}

df = [Link](d1)

df

[Link] 1/11
10/31/25, 11:12 AM DV [Link] - Colab

roll CNIP SE DV

0 1 4 5 44

1 2 12 28 14

2 3 18 6 14

3 4 37 10 44

4 5 33 4 30

5 6 30 40 37

6 7 24 51 14

7 8 11 40 7

8 9 29 21 25

9 10 12 19 28

import warnings
[Link]("ignore")
df["DV"].iloc[3:4]=0
df

roll CNIP SE DV

0 1 50 51 14

1 2 40 18 17

2 3 39 42 22

3 4 17 33 0

4 5 45 38 40

5 6 12 37 18

6 7 47 24 53

7 8 34 53 52

8 9 48 6 37

9 10 41 34 46

df['CNIP'].iloc[:] = 100
df

roll CNIP SE DV

0 1 100 51 14

1 2 100 18 17

2 3 100 42 22

3 4 100 33 0

4 5 100 38 40

5 6 100 37 18

6 7 100 24 53

7 8 100 53 52

8 9 100 6 37

9 10 100 34 46

df["SE"].loc[2:2]=0
df

[Link] 2/11
10/31/25, 11:12 AM DV [Link] - Colab

roll CNIP SE DV

0 1 100 51 14

1 2 100 18 17

2 3 100 0 22

3 4 100 33 0
df['SE'].loc[:] = 0
4
df 5 100 38 40

5 6 100 37 18
roll CNIP SE DV
6 7 100 24 53
0 1 100 0 14
7 8 100 53 52
1 2 100 0 17
8 9 100 6 37
2 3 100 0 22
9 10 100 34 46
3 4 100 0 0

4 5 100 0 40

5 6 100 0 18

6 7 100 0 53

7 8 100 0 52

8 9 100 0 37

9 10 100 0 46

keyboard_arrow_down Imputation

import numpy as np
import pandas as pd
d1 = {
"roll":[i for i in range(1,11)],
"CNIP": [Link](1,56,10),
"SE": [Link](1,56,10),
"DV": [Link](1,56,10)
}

df = [Link](d1)
df

roll CNIP SE DV

0 1 36 42 7

1 2 5 49 46

2 3 5 27 10

3 4 15 15 16

4 5 53 11 47

5 6 8 49 44

6 7 26 30 19

7 8 26 24 43

8 9 55 17 32

9 10 34 2 20

import numpy as np
df['DV'].loc[2:4] = [Link]
# df['DV'].iloc[2:5]
df

[Link] 3/11
10/31/25, 11:12 AM DV [Link] - Colab

roll CNIP SE DV

0 1 36 42 7.0

1 2 5 49 46.0

2 3 5 27 NaN

3 4 15 15 NaN

4 5 53 11 NaN

5 6 8 49 44.0

6 7 26 30 19.0

7 8 26 24 43.0

8 9 55 17 32.0

9 10 34 2 20.0

df['SE'].loc[5:7] = [Link]
df

roll CNIP SE DV

0 1 36 42.0 7.0

1 2 5 49.0 46.0

2 3 5 27.0 NaN

3 4 15 15.0 NaN

4 5 53 11.0 NaN

5 6 8 NaN 44.0

6 7 26 NaN 19.0

7 8 26 NaN 43.0

8 9 55 17.0 32.0

9 10 34 2.0 20.0

df['CNIP'].loc[:1] = [Link]
df

roll CNIP SE DV

0 1 NaN 42.0 7.0

1 2 NaN 49.0 46.0

2 3 5.0 27.0 NaN

3 4 15.0 15.0 NaN

4 5 53.0 11.0 NaN

5 6 8.0 NaN 44.0

6 7 26.0 NaN 19.0

7 8 26.0 NaN 43.0

8 9 55.0 17.0 32.0

9 10 34.0 2.0 20.0

[Link]()

<class '[Link]'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 roll 10 non-null int64
1 CNIP 8 non-null float64
2 SE 7 non-null float64
3 DV 7 non-null float64
dtypes: float64(3), int64(1)
memory usage: 452.0 bytes

[Link]()

[Link] 4/11
10/31/25, 11:12 AM DV [Link] - Colab

roll CNIP SE DV

0 False True False False

1 False True False False

2 False False False True

3 False False False True

4 False False False True

5 False False True False

6 False False True False

7 False False True False

8 False False False False

9 False False False False

[Link]().sum()

roll 0

CNIP 2

SE 3

DV 3

dtype: int64

keyboard_arrow_down Reshsaping DatFrame

import pandas as pd
df = [Link]({
"ID": [1, 1, 2, 2,1,2],
"Subject": ["Math", "Science", "Math", "Science", 'Math', 'Science'],
"Marks": [90, 85, 80, 95,100,200]
})

df1 = [Link]({
"ID": [1, 2, 3,4],
"Subject": ["Math", "Science", "Eng", "Phy"],
"Marks": [90, 85, 80, 95]
})

print(df)
print("*"*20)

pivot_data = [Link](index='ID', columns="Subject", values='Marks')


print(df1)
print("*"*20)
print(pivot_data)
print("*"*20)

pivot_table = pd.pivot_table(df, index="ID", columns="Subject", values="Marks", aggfunc="mean")


print(pivot_table)

import numpy as np
import pandas as pd

d1 = {
"roll":['one', 'one', 'one', 'two', 'two', 'two'],
"name":["ABC","PQR","XYZ", "ABC","PQR","XYZ"],
"DV": [22, 45, 35, 10, 20, 30],
"SE": [55, 65, 75, 85, 95, 15]
}

df = [Link](d1)

print(df)
print("*"*20)

print([Link](index='roll', columns='name', values='DV'))


print("*"*20)

[Link] 5/11
10/31/25, 11:12 AM DV [Link] - Colab

pt1 = pd.pivot_table(df, index='roll', columns='name', values='DV', aggfunc="mean")


print(pt1)
print("*"*20)

pt2 = pd.pivot_table(df, index='roll', columns='name', values= ['DV', "SE"], aggfunc="mean")


print(pt2)

keyboard_arrow_down Filtering Data

Def: It allows to extract specific rows based on conditions applied to one or more columns, making it easier to work with relevant
subsets of data

1. Using .loc accessor

import numpy as np
import pandas as pd
d1 = {
"roll":[i for i in range(1,11)],
"CNIP": [Link](1,56,10),
"SE": [Link](1,56,10),
"DV": [Link](1,56,10)
}

df = [Link](d1)
print(df)

roll CNIP SE DV
0 1 19 18 46
1 2 28 55 13
2 3 40 44 14
3 4 26 12 27
4 5 5 11 1
5 6 42 29 6
6 7 21 25 40
7 8 53 33 38
8 9 5 16 53
9 10 15 6 49

df['roll'].iloc[2:4]

roll CNIP SE DV

2 3 40 44 14

3 4 26 12 27

import pandas as pd
import numpy as np

df = [Link]({
'age' : [ 10, 22, 13, 21, 12, 11, 17],
'section' : [ 'A', 'B', 'C', 'B', 'B', 'A', 'A'],
'city' : [ 'Gurgaon', 'Delhi', 'Mumbai', 'Delhi', 'Mumbai', 'Delhi', 'Mumbai'],
'gender' : [ 'M', 'F', 'F', 'M', 'M', 'M', 'F'],
'salary' : [ 1000, 4000, 5000, 7000, 2000, 3000, 6000]
})

print(df)

age section city gender salary


0 10 A Gurgaon M 1000
1 22 B Delhi F 4000
2 13 C Mumbai F 5000
3 21 B Delhi M 7000
4 12 B Mumbai M 2000
5 11 A Delhi M 3000
6 17 A Mumbai F 6000

[Link][ (df['age'] > 20) & (df['salary'] > 2000)]

[Link] 6/11
10/31/25, 11:12 AM DV [Link] - Colab

age section city gender salary

1 22 B Delhi F 4000

3 21 B Delhi M 7000

[Link][([Link] == "B") & ([Link] == "F")]

age section city gender salary

1 22 B Delhi F 4000

keyboard_arrow_down 2. Using Stack and Unstack

import numpy as np
import pandas as pd
d1 = {
"roll":[i for i in range(1,11)],
"CNIP": [Link](1,56,10),
"SE": [Link](1,56,10),
"DV": [Link](1,56,10)
}

df = [Link](d1)

print(df)
print("*"*20)

df_stacked = [Link]()
print(df_stacked)
print("*"*20)

df_unstacked = df_stacked.unstack()
print(df_unstacked)

roll CNIP SE DV
0 1 51 17 32
1 2 23 31 19
2 3 26 2 50
3 4 38 33 15
4 5 2 17 42
5 6 31 4 40
6 7 33 2 4
7 8 9 10 48
8 9 40 49 10
9 10 42 43 18
********************
0 roll 1
CNIP 51
SE 17
DV 32
1 roll 2
CNIP 23
SE 31
DV 19
2 roll 3
CNIP 26
SE 2
DV 50
3 roll 4
CNIP 38
SE 33
DV 15
4 roll 5
CNIP 2
SE 17
DV 42
5 roll 6
CNIP 31
SE 4
DV 40
6 roll 7
CNIP 33
SE 2
DV 4
7 roll 8
CNIP 9
SE 10
DV 48

[Link] 7/11
10/31/25, 11:12 AM DV [Link] - Colab
8 roll 9
CNIP 40
SE 49
DV 10
9 roll 10
CNIP 42
SE 43
DV 18
dtype: int64
********************
roll CNIP SE DV
0 1 51 17 32
1 2 23 31 19
2 3 26 2 50

keyboard_arrow_down Using Melt() method

df_melt = [Link](id_vars=['roll'], value_vars=['SE', 'DV'])


print(df_melt)

roll variable value


0 1 SE 18
1 2 SE 55
2 3 SE 44
3 4 SE 12
4 5 SE 11
5 6 SE 29
6 7 SE 25
7 8 SE 33
8 9 SE 16
9 10 SE 6
10 1 DV 46
11 2 DV 13
12 3 DV 14
13 4 DV 27
14 5 DV 1
15 6 DV 6
16 7 DV 40
17 8 DV 38
18 9 DV 53
19 10 DV 49

import [Link] as plt


import seaborn as sns
data = sns.load_dataset('tips')

# 4. Pair Plot
[Link](data, hue="smoker")
[Link]()

[Link] 8/11
10/31/25, 11:12 AM DV [Link] - Colab

# 5. Line Plot
import seaborn as sns
import [Link] as plt

flights = sns.load_dataset("flights")
[Link](x="year", y="passengers", data=flights, color="blue")
[Link]("Line Plot: Year vs Number of Passengers")
[Link]()

# 6. count Plot
import seaborn as sns
import [Link] as plt

titanic = sns.load_dataset("titanic")

[Link] 9/11
10/31/25, 11:12 AM DV [Link] - Colab
[Link](x="class", data=titanic, palette="pastel")
[Link]("Count Plot: Number of Passengers by Class")
[Link]()

/tmp/[Link]: FutureWarning:

Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and

[Link](x="class", data=titanic, palette="pastel")

[Link]()

survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone

0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False

1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False

2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True

3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False

4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

import [Link] as plt


import pandas as pd
titanic = sns.load_dataset("titanic")
df1= titanic['class'].value_counts().to_dict()
# df2= [Link](df1)
print(df1)

# [Link](df1)
# [Link]("histogram")
# [Link]()

# [Link](x="class", )

{'Third': 491, 'First': 216, 'Second': 184}

[Link](df1, columns=["Third", "First", "Second"], index=[1,2,3])

Third First Second

1 491 216 184

2 491 216 184

3 491 216 184

import pandas as pd
import seaborn as sns
import [Link] as plt

iris = sns.load_dataset("iris")

[Link] 10/11
10/31/25, 11:12 AM DV [Link] - Colab
d1 = [Link].value_counts().to_dict()
[Link]([Link](), [Link]())

<BarContainer object of 3 artists>

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
import yfinance as yf

# Download data from Yahoo Finance


data = [Link]('[Link]', start='2020-01-01', end='2023-12-31')

# View first few rows


data.to_excel(Relience)

/tmp/[Link]: FutureWarning: [Link]() has changed argument auto_adjust default to True


data = [Link]('[Link]', start='2020-01-01', end='2023-12-31')

[Link] 11/11

You might also like