【创新实训】数据预处理(四)

从movie中取相同电影的映射关系,然后从details中取这些id对应的信息,id重新设置从0开始

import pandas as pd
import pymongo
from setting import setting

db = pymongo.MongoClient(host=setting['host'])['movie'] //连接数据库
db.authenticate(setting['username'], setting['password'])//用户验证
//excludes = ['insertStamp', 'timestamp', 'rating', 'rateNum', 'cover']
//excludes = {v: False for v in excludes}
//data = db['details'].find({}, excludes)
//df = pd.DataFrame.from_records(data)
//df.to_csv('details.csv', index=False)

doubanIds = []
table = {'maoyan': {}, 'mtime': {}}     //otherSrcId -> doubanId

//只需要source字段的数据
for doc in db['movie'].find({}, {'source': True}, no_cursor_timeout=True):
    doubanId = doc['source']['douban']['sourceId']
    doubanIds.append(doubanId)
    for src in ['maoyan', 'mtime']:
        if src in doc['source']:
            table[src][doc['source'][src]['sourceId']] = doubanId
print('douban num: ', len(doubanIds))

details = pd.read_csv('details.csv', index=False)
douban = details[(details['source'] == 'douban') & details['sourceId'].isin(doubanIds)]
maoyan = details[(details['source'] == 'maoyan') & details['sourceId'].isin(table['maoyan'].keys())]
mtime = details[(details['source'] == 'mtime') & details['sourceId'].isin(table['mtime'].keys())]

df = douban.append(maoyan).append(mtime).reset_index()
//拼接起来,重新设置index,从0开始,作为新的id
trans = {'douban': {}, 'maoyan': {}, 'mtime': {}}
for id, row in df.iterrows():
    trans[row['source']][str(row['sourceId'])] = id     # srcId -> newId

with open('link.csv', 'w') as f:
    for src in ['maoyan', 'mtime']:
        for k, v in table[src].items():
            f.write("{}\t{}\n".format(trans[src][k], trans['douban'][v]))   # otherNewId, doubanNewId

df['id'] = df.index
df.drop(columns=['imdb', 'source', 'sourceId']).to_csv('data.csv', index=False)
//drop去掉不需要的列


输出link.csv每行表示其他来源和douban的这个id是同一个电影
data.csv是link中电影对应地信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值