-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData Wrangling - Replace with Frequent Occuring data
28 lines (20 loc) · 1.26 KB
/
Data Wrangling - Replace with Frequent Occuring data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pandas as pd
import matplotlib.pylab as plt
import numpy as np
url = r"https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
df = pd.read_csv(url, header=None)
headers = ["symboling","normalized-losses","make","fuel-type","aspiration", "num-of-doors","body-style",
"drive-wheels","engine-location","wheel-base", "length","width","height","curb-weight","engine-type",
"num-of-cylinders", "engine-size","fuel-system","bore","stroke","compression-ratio","horsepower",
"peak-rpm","city-mpg","highway-mpg","price"]
df.columns = headers
#replace data with "?" with NAN so it will be treat as Null
df.replace("?", np.nan, inplace = True)
#identify the frequent occuring data by using the value.counts() and idxmax().
#value.counts() will list the data and its frequency to appear, and add idmax() will identify the most frequent occuring data from this list.
df['num-of-doors'].value_counts()
df['num-of-doors'].value_counts().idxmax()
#print(df['num-of-doors'].value_counts().idxmax()) --> to check
#since we know the most frequent occuring data is "four" then we will replace the missing value in column "num-of-doors" with "four".
df["num-of-doors"]=df["num-of-doors"].replace(np.nan, "four")
print(df["num-of-doors"].head(50))