【发布时间】:2018-08-27 01:39:58
【问题描述】:
我有一个元组列表,其中包含有关评论(字符串文本)和评论发布日期的信息。例如:
comments[1]
(datetime.date(2016, 8, 29),
'I played with these ATM before but they are just too expensive way to buy bitcoins.There were a few in the city I live but many of them already stop operation, most likely because no one actually uses them.')
我有函数lda_description,它返回元组列表(topic, value)、topic 是从 1 到 40 的数字,返回的列表长度也是从 1 到 40,例如:
lda_description(comments[1][1])
[(10, 0.43287377217078077), (14, 0.43712141484779793), (21, 0.068338146314754045)]
问题是我希望lda_description 结果映射到具有 40 列主题且索引为日期时间的 pandas 数据框。数据框字段值应该是每个主题在特定日期的所有 cmets 的 lda_description 的总和。
我的解决方案在我看来效率不高,也许有人可以帮助我:)
#Creating empty dataframe
df = pd.DataFrame(0, index=pd.date_range(datetime.datetime(2013,12,1), datetime.datetime(2016,11,21)).tolist(),
columns=range(1,41))
df["count"] = 0
i = 0
for com in comments:
if i % 50000 == 0:
print(datetime.datetime.now(), i)
i += 1
topic_dist = lda_description(com[1])
for dist in topic_dist:
df.set_value(com[0],dist[0],
df.ix[com[0]][dist[0]] + dist[1])
df.set_value(com[0],'count',
df.ix[com[0]]['count'] + 1)
【问题讨论】:
标签: python-3.x pandas