Question
Please provide python code to perform one hot encoding for below 6 categorical variables in dataset df cat = ['Location_type', 'WH_capacity_size', 'zone', 'WH_regional_zone', 'wh_owner_type', 'approved_wh_govt_certificate']
Please provide python code to perform one hot encoding for below 6 categorical variables in dataset df
cat = ['Location_type', 'WH_capacity_size', 'zone', 'WH_regional_zone', 'wh_owner_type', 'approved_wh_govt_certificate']
#Replace Location_type Rural with 1 and Urban with 2
df['Location_type'] = df['Location_type'].replace({'Rural' : 1, 'Urban' :2})
#Replace WH_capacity_size Large with 2, Medium with 1 and Small with 0
df['WH_capacity_size'] = df['WH_capacity_size'].replace({'Large' :2, 'Mid' :1, 'Small' :0})
#Replace Zone East with 1, West with 2, North with 3 and South with 4
df['zone'] = df['zone'].replace({'East' : 1, 'West' :2, 'North' : 3, 'South' :4})
#Replace WH_regional_zone zone1 with 1, zone2 with 2,zone3 with 3,zone4 with 4 and zone5 with 5
df['WH_regional_zone'] = df['WH_regional_zone'].replace({'Zone 1' : 1, 'Zone 2' :2, 'Zone 3' :3, 'Zone 4' :4,'Zone 5' :5, 'Zone 6' :6})
#Replace wh_owner_type Company Owned with 1 and Rented with 2
df['wh_owner_type'] = df['wh_owner_type'].replace({'Company Owned' : 1, 'Rented' :2})
#Replace approved_wh_govt_certificate A+ with 5, A with 4, B+ with 3, B with 2, C with 1 and NA with 0
df['approved_wh_govt_certificate'] = df['approved_wh_govt_certificate'].replace({'A+' : 5, 'A' :4, 'B+' : 3, 'B' :2, 'C' : 1, 'NA' : 0})
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started