I am working with a dataframe, and a few data columns have missing catagories represented by a '?' in a column. I am trying to use a boolean to rename and replace the missing catagories marked '?' with 'Private'in a column labled workclass. The data is read in as:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import *
url2="https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" #Reading in Data from a freely and easily available source on the internet
Adult = pd.read_csv(url2, header=None)
##Assigning column names to the dataframe
Adult.columns = ["age","workclass","fnlwgt","education","educationnum","maritalstatus","occupation",
"relationship","race","sex","capitalgain","capitalloss","hoursperweek","nativecountry",
"less50kmoreeq50kn"]
I have tried running the code:
MissingValue = Adult.loc[:, "workclass"] == "?"
Adult.loc[MissingValue, "workclass"] = "Private"
and
Adult.loc[ Adult.loc[:, "workclass"] == "?", "workclass"] = "Private"
I do not get any error's when running the code however when checking the column values with (Adult.loc[:,'workclass'].value_counts()) the '?' is still there.
The code: Adult['workclass'] = Adult['workclass'].str.replace('?', 'Private') works for what I want to accomplish however I would like to be able to do it with a boolean. Any suggestions on why this might be happening?