我有這個資料集:

這是請求:“將 Mjob 和 Fjob 屬性添加到必須使用您選擇的轉換公式映射其分類值的“數字”資料集。”
有誰知道該怎么做?例如:如果“at_home”值在 Mjob 中變為“1”,我希望 Fjob 列中的結果相同。相同的分類值必須具有相同的整數值轉換。
謝謝你們。
uj5u.com熱心網友回復:
您可以將該map函式與 pandas Series/Column 一起使用,將分類變數從字串資料映射到數值資料。例如,使用以下 pandas 資料框:
data = np.array([
['at_home','teacher'],
['at_home','other'],
['at_home','other'],
['health', 'services']
])
df = pd.DataFrame(data=data, columns=['Mjob', 'Fjob'])
map使用該函式創建了兩個新列
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424184.html
