0% found this document useful (0 votes)
5 views27 pages

Machine Learning Using Python Praticals

The document contains multiple practical exercises using Pandas to handle missing values in DataFrames. It includes code examples for detecting missing values, identifying columns with missing values, counting missing values, replacing specific placeholders with NaN, and filling NaNs with a constant value. Each exercise is accompanied by sample output demonstrating the results of the operations performed.

Uploaded by

sebgowtham
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)
5 views27 pages

Machine Learning Using Python Praticals

The document contains multiple practical exercises using Pandas to handle missing values in DataFrames. It includes code examples for detecting missing values, identifying columns with missing values, counting missing values, replacing specific placeholders with NaN, and filling NaNs with a constant value. Each exercise is accompanied by sample output demonstrating the results of the operations performed.

Uploaded by

sebgowtham
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

PRATICAL NUMBER 4

Write a Pandas program to detect missing values of a given DataFrame. Display


True or False.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45,
75.29,3045.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nMissing values of the said dataframe:")

print([Link]())

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
1 NaN 270.65 2012-09-10 3001 5003.0
2 70002.0 65.26 NaN 3001 5001.0
3 70004.0 110.50 2012-08-17 3003 NaN
4 NaN 948.50 2012-09-10 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
6 NaN 5760.00 2012-09-10 3001 5001.0
7 70010.0 1983.43 2012-10-10 3004 NaN
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
10 NaN 75.29 2012-08-17 3001 5003.0
11 70013.0 3045.60 2012-04-25 3001 NaN
Missing values of the said dataframe:
ord_no purch_amt ord_date customer_id salesman_id
0 False False False False False
1 True False False False False
2 False False True False False
3 False False False False True
4 True False False False False
5 False False False False False
6 True False False False False
7 False False False False True
8 False False False False False
9 False False False False False
10 True False False False False
11 False False False False True

PRATICAL NUMBER 5

Write a Pandas program to identify the column(s) of a given DataFrame which


have at least one missing value.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45,
75.29,3045.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],
'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nIdentify the columns which have at least one missing value:")

print([Link]().any())

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
1 NaN 270.65 2012-09-10 3001 5003.0
2 70002.0 65.26 NaN 3001 5001.0
3 70004.0 110.50 2012-08-17 3003 NaN
4 NaN 948.50 2012-09-10 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
6 NaN 5760.00 2012-09-10 3001 5001.0
7 70010.0 1983.43 2012-10-10 3004 NaN
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
10 NaN 75.29 2012-08-17 3001 5003.0
11 70013.0 3045.60 2012-04-25 3001 NaN
Identify the columns which have at least one missing value:
ord_no True
purch_amt False
ord_date True
customer_id False
salesman_id True
dtype: bool

PRATICAL NUMBER 6

Write a Pandas program to count the number of missing values in each column
of a given DataFrame.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)
#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45,
75.29,3045.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nNumber of missing values of the said dataframe:")

print([Link]().sum())

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
1 NaN 270.65 2012-09-10 3001 5003.0
2 70002.0 65.26 NaN 3001 5001.0
3 70004.0 110.50 2012-08-17 3003 NaN
4 NaN 948.50 2012-09-10 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
6 NaN 5760.00 2012-09-10 3001 5001.0
7 70010.0 1983.43 2012-10-10 3004 NaN
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
10 NaN 75.29 2012-08-17 3001 5003.0
11 70013.0 3045.60 2012-04-25 3001 NaN

Number of missing values of the said dataframe:


ord_no 4
purch_amt 0
ord_date 1
customer_id 0
salesman_id 3
dtype: int64
PRATICAL NUMBER 7
Write a Pandas program to find and replace the missing values in a given
DataFrame which do not have any valuable information.

Example:
Missing values: ?, --
Replace those values with NaN

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,"--
",70010,70003,70012,[Link],70013],

'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,"?",12.43,2480.4,250.45,
3045.6],

'ord_date': ['?','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-27','2012-09-
10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,"--",3002,3001,3001],

'salesman_id':[5002,5003,"?",5001,[Link],5002,5001,"?",5003,5002,5003,"--"]})

print("Original Orders DataFrame:")

print(df)

print("\nReplace the missing values with NaN:")

result = [Link]({"?": [Link], "--": [Link]})

print(result)

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001 150.5 ? 3002 5002
1 NaN 270.65 2012-09-10 3001 5003
2 70002 65.26 NaN 3001 ?
3 70004 110.5 2012-08-17 3003 5001
4 NaN 948.5 2012-09-10 3002 NaN
5 70005 2400.6 2012-07-27 3001 5002
6 -- 5760 2012-09-10 3001 5001
7 70010 ? 2012-10-10 3004 ?
8 70003 12.43 2012-10-10 -- 5003
9 70012 2480.4 2012-06-27 3002 5002
10 NaN 250.45 2012-08-17 3001 5003
11 70013 3045.6 2012-04-25 3001 --
Replace the missing values with NaN:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 NaN 3002.0 5002.0
1 NaN 270.65 2012-09-10 3001.0 5003.0
2 70002.0 65.26 NaN 3001.0 NaN
3 70004.0 110.50 2012-08-17 3003.0 5001.0
4 NaN 948.50 2012-09-10 3002.0 NaN
5 70005.0 2400.60 2012-07-27 3001.0 5002.0
6 NaN 5760.00 2012-09-10 3001.0 5001.0
7 70010.0 NaN 2012-10-10 3004.0 NaN
8 70003.0 12.43 2012-10-10 NaN 5003.0
9 70012.0 2480.40 2012-06-27 3002.0 5002.0
10 NaN 250.45 2012-08-17 3001.0 5003.0
11 70013.0 3045.60 2012-04-25 3001.0 NaN
PRATICAL NUMBER 8
Write a Pandas program to replace NaNs with a single constant value in
specified columns in a DataFrame

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45,
75.29,3045.6],
'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nReplace NaNs with a single constant value:")

result = df['ord_no'].fillna(0, inplace=False)

print(result)

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
1 NaN 270.65 2012-09-10 3001 5003.0
2 70002.0 65.26 NaN 3001 5001.0
3 70004.0 110.50 2012-08-17 3003 NaN
4 NaN 948.50 2012-09-10 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
6 NaN 5760.00 2012-09-10 3001 5001.0
7 70010.0 1983.43 2012-10-10 3004 NaN
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
10 NaN 75.29 2012-08-17 3001 5003.0
11 70013.0 3045.60 2012-04-25 3001 NaN

Replace NaNs with a single constant value:


0 70001.0
1 0.0
2 70002.0
3 70004.0
4 0.0
5 70005.0
6 0.0
7 70010.0
8 70003.0
9 70012.0
10 0.0
11 70013.0
Name: ord_no, dtype: float64
PRATICAL NUMBER 9
Write a Pandas program to replace NaNs with median or mean of the specified
columns in a given DataFrame.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,[Link],65.26,110.5,948.5,[Link],5760,1983.43,[Link],250.45,
75.29,3045.6],

'sale_amt':[10.5,20.65,[Link],11.5,98.5,[Link],57,19.43,[Link],25.45, 75.29,35.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("Using median in purch_amt to replace NaN:")

df['purch_amt'].fillna(df['purch_amt'].median(), inplace=True)

print(df)

print("Using mean to replace NaN:")

df['sale_amt'].fillna(int(df['sale_amt'].mean()), inplace=True)
print(df)

output: -
Original Orders DataFrame:
ord_no purch_amt ... customer_id salesman_id
0 70001.0 150.50 ... 3002 5002.0
1 NaN NaN ... 3001 5003.0
2 70002.0 65.26 ... 3001 5001.0
3 70004.0 110.50 ... 3003 NaN
4 NaN 948.50 ... 3002 5002.0
5 70005.0 NaN ... 3001 5001.0
6 NaN 5760.00 ... 3001 5001.0
7 70010.0 1983.43 ... 3004 NaN
8 70003.0 NaN ... 3003 5003.0
9 70012.0 250.45 ... 3002 5002.0
10 NaN 75.29 ... 3001 5003.0
11 70013.0 3045.60 ... 3001 NaN
[12 rows x 6 columns]
Using median in purch_amt to replace NaN:
ord_no purch_amt ... customer_id salesman_id
0 70001.0 150.50 ... 3002 5002.0
1 NaN 250.45 ... 3001 5003.0
2 70002.0 65.26 ... 3001 5001.0
3 70004.0 110.50 ... 3003 NaN
4 NaN 948.50 ... 3002 5002.0
5 70005.0 250.45 ... 3001 5001.0
6 NaN 5760.00 ... 3001 5001.0
7 70010.0 1983.43 ... 3004 NaN
8 70003.0 250.45 ... 3003 5003.0
9 70012.0 250.45 ... 3002 5002.0
10 NaN 75.29 ... 3001 5003.0
11 70013.0 3045.60 ... 3001 NaN

[12 rows x 6 columns]


Using mean to replace NaN:
ord_no purch_amt ... customer_id salesman_id
0 70001.0 150.50 ... 3002 5002.0
1 NaN 250.45 ... 3001 5003.0
2 70002.0 65.26 ... 3001 5001.0
3 70004.0 110.50 ... 3003 NaN
4 NaN 948.50 ... 3002 5002.0
5 70005.0 250.45 ... 3001 5001.0
6 NaN 5760.00 ... 3001 5001.0
7 70010.0 1983.43 ... 3004 NaN
8 70003.0 250.45 ... 3003 5003.0
9 70012.0 250.45 ... 3002 5002.0
10 NaN 75.29 ... 3001 5003.0
11 70013.0 3045.60 ... 3001 NaN

[12 rows x 6 columns]


PRATICAL NUMBER 10
Write a Pandas program to replace the missing values with the most frequent
values present in each column of a given dataframe.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],

'purch_amt':[150.5,[Link],65.26,110.5,948.5,[Link],5760,1983.43,[Link],250.45,
75.29,3045.6],

'sale_amt':[10.5,20.65,[Link],11.5,98.5,[Link],57,19.43,[Link],25.45, 75.29,35.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nReplace the missing values with the most frequent values present in each
column:")

result = [Link]([Link]().iloc[0])

print(result)

output: -
Original Orders DataFrame:
ord_no purch_amt ... customer_id salesman_id
0 70001.0 150.50 ... 3002 5002.0
1 NaN NaN ... 3001 5003.0
2 70002.0 65.26 ... 3001 5001.0
3 70004.0 110.50 ... 3003 NaN
4 NaN 948.50 ... 3002 5002.0
5 70005.0 NaN ... 3001 5001.0
6 NaN 5760.00 ... 3001 5001.0
7 70010.0 1983.43 ... 3004 NaN
8 70003.0 NaN ... 3003 5003.0
9 70012.0 250.45 ... 3002 5002.0
10 NaN 75.29 ... 3001 5003.0
11 70013.0 3045.60 ... 3001 NaN
[12 rows x 6 columns]

Replace the missing values with the most frequent values present in
each column:
ord_no purch_amt ... customer_id salesman_id
0 70001.0 150.50 ... 3002 5002.0
1 70001.0 65.26 ... 3001 5003.0
2 70002.0 65.26 ... 3001 5001.0
3 70004.0 110.50 ... 3003 5001.0
4 70001.0 948.50 ... 3002 5002.0
5 70005.0 65.26 ... 3001 5001.0
6 70001.0 5760.00 ... 3001 5001.0
7 70010.0 1983.43 ... 3004 5001.0
8 70003.0 65.26 ... 3003 5003.0
9 70012.0 250.45 ... 3002 5002.0
10 70001.0 75.29 ... 3001 5003.0
11 70013.0 3045.60 ... 3001 5001.0

[12 rows x 6 columns]


PRATICAL NUMBER 11
Write a Pandas program to drop the rows where at least one element is missing
in a given DataFrame.

Python code: -

import pandas as pd

import numpy as np

pd.set_option('display.max_rows', None)

#pd.set_option('display.max_columns', None)

df = [Link]({

'ord_no':[70001,[Link],70002,70004,[Link],70005,[Link],70010,70003,70012,[Link]
n,70013],
'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45,
75.29,3045.6],

'ord_date': ['2012-10-05','2012-09-10',[Link],'2012-08-17','2012-09-10','2012-07-
27','2012-09-10','2012-10-10','2012-10-10','2012-06-27','2012-08-17','2012-04-25'],

'customer_id':[3002,3001,3001,3003,3002,3001,3001,3004,3003,3002,3001,3001],

'salesman_id':[5002,5003,5001,[Link],5002,5001,5001,[Link],5003,5002,5003,np.n
an]})

print("Original Orders DataFrame:")

print(df)

print("\nDrop the rows where at least one element is missing:")

result = [Link]()

print(result)

output: -
Original Orders DataFrame:
ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
1 NaN 270.65 2012-09-10 3001 5003.0
2 70002.0 65.26 NaN 3001 5001.0
3 70004.0 110.50 2012-08-17 3003 NaN
4 NaN 948.50 2012-09-10 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
6 NaN 5760.00 2012-09-10 3001 5001.0
7 70010.0 1983.43 2012-10-10 3004 NaN
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
10 NaN 75.29 2012-08-17 3001 5003.0
11 70013.0 3045.60 2012-04-25 3001 NaN

Drop the rows where at least one element is missing:


ord_no purch_amt ord_date customer_id salesman_id
0 70001.0 150.50 2012-10-05 3002 5002.0
5 70005.0 2400.60 2012-07-27 3001 5001.0
8 70003.0 2480.40 2012-10-10 3003 5003.0
9 70012.0 250.45 2012-06-27 3002 5002.0
PRATICAL NUMBER 12
Write a Pandas program to import given excel data ([Link] ) into a
Pandas dataframe
Python code: -

import pandas as pd

import numpy as np

df = pd.read_excel('E:\[Link]')

print([Link])

output: -

Year MSHA ID Mine_Name Production Labor_Hours

0 2013 103381 Tacoa Highwall Miner 56004 22392

1 2013 103404 Reid School Mine 28807 28447

2 2013 100759 North River #1 Underground Min 1440115 474784

3 2013 103246 Bear Creek 87587 29193

4 2013 103451 Knight Mine 147499 46393

5 2013 103433 Crane Central Mine 69339 47195

6 2013 100329 Concord Mine 0 144002

7 2013 100851 Oak Grove Mine 2269014 1001809

8 2013 102901 Shoal Creek Mine 0 12396

9 2013 102901 Shoal Creek Mine 1453024 1237415

10 2013 103180 Sloan Mountain Mine 327780 196963

11 2013 103182 Fishtrap 175058 87314

12 2013 103285 Narley Mine 154861 90584

13 2013 103332 Powhatan Mine 140521 61394

14 2013 103375 Johnson Mine 580 1900

15 2013 103419 Maxine-Pratt Mine 125824 107469


16 2013 103432 Skelton Creek 8252 220

17 2013 103437 Black Warrior Mine No 1 145924 70926

18 2013 102976 Piney Woods Preparation Plant 0 14828

19 2013 102976 Piney Woods Preparation Plant 0 23193

20 2013 103380 Calera 0 12621

21 2013 103380 Calera 0 1402

22 2013 103422 Clark No 1 Mine 122727 140250

23 2013 103467 Helena Surface Mine 59664 30539

24 2013 101247 No 4 Mine 2622528 1551141

25 2013 101401 No 7 Mine 5405412 2464719

You might also like