Issue
I have this dataset:
This is the request: "Add the Mjob and Fjob attributes to the “numeric” dataset whose categorical value must be mapped using a conversion formula of your choice."
Does anyone knows how to do it? For example: if 'at_home' value become '1' in Mjob, I want the same result in the Fjob column. Same categorical values must have the same integer values transformation.
Solution
You can use the map
function with a pandas Series/Column to map a categorical variable from string data to numeric data. For example, with the following pandas dataframe:
data = np.array([
['at_home','teacher'],
['at_home','other'],
['at_home','other'],
['health', 'services']
])
df = pd.DataFrame(data=data, columns=['Mjob', 'Fjob'])
two new columns are created with the map
function
map_dict = {'at_home':1, 'teacher':2, 'other':3, 'health':4, 'services':5}
df['Mjob_numeric'] = df['Mjob'].map(map_dict)
df['Fjob_numeric'] = df['Fjob'].map(map_dict)
Answered By - jomavera
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.