| # author:Dragon少年 # 导入爬取得到的数据 df = pd.read_csv("月饼.csv", encoding='utf-8-sig', header=None) df.columns = ["商品名", "价格", "购买人数", "店铺", "地址"] # 去除重复的数据 df.drop_duplicates(inplace=True) print(df.shape) # 删除购买人数0的记录 df['购买人数'] = df['购买人数'].replace(np.nan,'0人付款')   df['num'] = [re.findall(r'(\d+\.{0,1}\d*)', i)[0] for i in df['购买人数']]  # 提取数值 df['num'] = df['num'].astype('float')  # 转化数值型 # 提取单位(万) df['unit'] = [''.join(re.findall(r'(万)', i)) for i in df['购买人数']]  # 提取单位(万) df['unit'] = df['unit'].apply(lambda x:10000 if x=='万' else 1) # 计算销量 df['销量'] = df['num'] * df['unit']   # 删除没有发货地址的店铺数据 获取省份 df = df[df['地址'].notna()] df['省份'] = df['地址'].str.split(' ').apply(lambda x:x[0]) # 删除多余的列 df.drop(['购买人数', '地址', 'num', 'unit'], axis=1, inplace=True) # 重置索引 df = df.reset_index(drop=True) df.to_csv('月饼清洗数据.csv') |