【发布时间】:2020-03-25 19:28:50
【问题描述】:
我有一个数据集,其中包含 reddit 的 NameAccount 以及他们随时间和 subreddit 编写的消息。像这样:
对于我的porpuse,我需要一个带有[帐户名称,他写的所有消息]的数组(因为正文(看图片)只有一条消息,但是如果我们看到所有作者,就会出现重复) .
所以我写了这个程序:
test_data = pd.read_csv("addres/test_data.csv", encoding="utf8")
test = test_data[['author', 'body']]
lista = [list(x) for x in test.values]
test=dict()
for i in range(1107946):
if lista[i][0] in test:
test[lista[i][0]].append(lista[i][1])
else:
test[lista[i][0]]=[lista[i][1]]
我得到了我喜欢的东西。 如果我写 test["Name"] 我会得到那个人的所有消息。 例如:
test["ZenDragon"]
['At 7680 by 4320 with 64x AA, right?', 'Wrong subreddit for this kind of post, but /r/frugal and /r/lifeprotips might be interested.', 'This is something GravityBox can do. (a module for XPosed Framework)',etc]
现在我想加入所有这些行。 例如:["message1","message2","message3",etc..] -> ["message 1 message 2 etc..."] 我试过写这个东西:
for i in test.keys():
X.append(" ".join(line.strip() for line in test[i]))
但是我有这个错误: 'float' 对象没有属性 'strip'
但我没有浮动对象?
【问题讨论】:
-
如果您遇到该错误,
line必须 在某些时候是一个浮点数。仔细检查您的数据。
标签: python arrays dictionary join strip