天池新人赛_构造次日购买特征

#构造次日购买特征

#导入库文件
print('构造次日购买特征')
import pandas
import numpy
from pandas import read_csv
from pandas import Series
#读取并设置数据表
df=read_csv('D:\\sample.csv',low_memory=False)
df=df.drop(columns= ['Unnamed: 0'],axis=1)
del df['购买商品']
print(df)
#删除无用列
n=df.loc[df['日'].isin([18])]
n=n.loc[df['购买'].isin([1])]
n=n.drop_duplicates()
#重置索引
a=numpy.arange(0,len(n['用户标识']),1)
n.index=a

#只留下商品标识,用户标识,购买标记

del n['购买']
del n['日']
del n['收藏']
del n['浏览']
del n['加购物车']
del n['商品分类']
del n['用户行为']
'''
m.rename(columns={'商品标识':'商品标识1'},inplace=True)
n.rename(columns={'商品标识':'商品标识1'},inplace=True)
'''
group=df.loc[df['日'].isin([14,15,16,17,18])]
print(group)

a=numpy.arange(0,len(group['用户标识']),1)
group.index=a

#删除商品标识不同的行
i = 0
j = 0
d = len(group['用户标识'])
l = len(n['用户标识'])
a = numpy.zeros(group.shape[0])
while i<d:
     j=0
     while j<l:
          if  group['用户标识'][i]==n['用户标识'][j]:
               if group['商品标识'][i]==n['商品标识'][j]:
                    a[i]=1
          j=j+1
     i=i+1
group.insert(9,'18日购买',a)#先创建再并入数据框

print(n)
print(group)
print(sum(group['18日购买']))
group.to_csv('D:\\sample_1.3.csv')

猜你喜欢

转载自blog.csdn.net/WHUT618/article/details/82987457