1.df表格一列按内容拆分成多列
采用onehot拆分
df1 = pd.get_dummies(df_test['product_category'])
df_test = pd.concat([df_test,df1],axis=1)
for cg in range(1,19):
df_test[cg] = df_test[cg]*df_test['click_times']
df_test=df_test.drop(['product_category','click_times'], 1, inplace = False)
#df_test.head(15)
由于数据量大,>3000万样本,所以单独累加,合并成100万,
上次有自己写代码试过,时间将近(4)h.。。
df_test = df_test.groupby(['user_id']).sum()
refer:python中dataframe将一列中的数值拆分成多个列
2.多行合并为一行
# 设置Customer_ID为索引,month为列名,统计每个月的购买次数
pivoted_counts = df.pivot_table(index="Customer_ID",
columns='month',
values='Order_Date',
aggfunc='count').fillna(0)
#大于1次的全部设为1
df_purchase = pivoted_counts.applymap(lambda x:1 if x>0 else 0)
df_purchase.head()