简体   繁体   中英

Add features to the “numeric” dataset whose categorical value must be mapped using a conversion formula

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM