我有具有分類和數字特征的資料集。我想使用 for 回圈將我的分類特征轉換為數字形式,該回圈僅遍歷分類變數并將它們轉換為數字形式。
我試過這個
for i in df_balanced.columns[:-1]:
if (df_balanced[i].dtype == 'object'):
df_new = pd.get_dummies(i[:], columns=i[:], drop_first = True)
但它給了我空的資料框
uj5u.com熱心網友回復:
利用:
#get DataFrame with categorical (non numeric columns)
df1 = df_balanced.iloc[:, :-1].select_dtypes(object)
#remove categorical and join dummies
df = df.drop(df1.columns, axis=1).join(pd.get_dummies(df1, drop_first = True))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/525428.html
標籤:Python熊猫
下一篇:左連接表而不復制右表行值
