logo
Loading...

群聚編碼問題? - Cupoy

程式碼:# 取一個類別型欄位, 與一個數值型欄位, 做群聚編碼df['Sex'] = df['Sex...

ml100-3

群聚編碼問題?

2019/10/17 01:52 PM
機器學習新手論壇
嚴詩豪
觀看數:0
回答數:4
收藏數:0
ml100-3

程式碼:

# 取一個類別型欄位, 與一個數值型欄位, 做群聚編碼

df['Sex'] = df['Sex'].fillna('None')

mean_df = df.groupby(['Sex'])['Age'].mean().reset_index()

mode_df = df.groupby(['Sex'])['Age'].apply(lambda x: x.mode()).reset_index()

median_df = df.groupby(['Sex'])['Age'].median().reset_index()

max_df = df.groupby(['Sex'])['Age'].max().reset_index()

temp = pd.merge(mean_df, mode_df,how='left',on=['Sex'])

temp = pd.merge(temp, median_df,how='left',on=['Sex'])

temp = pd.merge(temp, max_df,how='left',on=['Sex'])

temp.columns = ['Sex','Cabin_age_Mean', 'Cabin_age_Mode', 'Cabin_age_Median', 'Cabin_age_Max','p']

temp

我這樣用出來的欄位怎麼會有六個?