【发布时间】:2018-03-22 13:45:04
【问题描述】:
我正在对 crpytocurrency 进行情绪分析。我的工作是清理 csv 文件中的数据。生成数据(来自 Twitter)并保存在 csv 文件中。在做情绪分析部分之前。我必须清理数据。例如,删除标点符号、URL,将测试放在小写中。这些是推文。
## 我已经导入了有用的库,例如 NLTK(自然语言处理)、pandas、numpy 等。
这是“推文”列的输出。
ctweet['Tweets'][0:6]
Out[5]:
0 RT @TheLTCnews: The @LTCFoundation has publish...
1 RT @WildchildSings: "https:/ " + /t.co/"FZrGw6xsZU ac..."
2 RT @HODL_Whale: 5 days until #LitePay launches...
3 LTC to USD price $211.92 "https:/" + /t.co/"CFjg1mIg..."
4 LTC to BTC price B0.020218 "https:/" +/t.co/"XPL8NI..."
5 LTC to GBP price £151.89 "https:/" +/t.co/"iOIbhgyd..."
6 Litecoin dropped into the bear zone as sugges...
Name: Tweets, dtype: object
# the output contains url. Because stackoverflow won't allow me to post the url. I have to change the method for url like adding "quotes" and "//".
我的下一个任务是清理数据。这是预处理代码。
#Preprocessing del RT @blablabla:
ctweet['tweetos'] = ''
#add tweetos first part
for i in range(len(ctweet['Tweets'])):
try:
ctweet['tweetos'][i] = ctweet['Tweets'].str.split(' ')[i][0]
except AttributeError:
ctweet['tweetos'][i] = 'other'
#Preprocessing tweetos. select tweetos contains 'RT @'
for i in range(len(ctweet['Tweets'])):
if ctweet['tweetos'].str.contains('@')[i] == False:
ctweet['tweetos'][i] = 'other'
# remove URLs, RTs, and twitter handles
for i in range(len(ctweet['Tweets'])):
ctweet['Tweets'][i] = " ".join([word for word in ctweet['Tweets'][i].split()
if 'http' not in word and '@' not in word and '<' not in word])
ctweet['Tweets'][0]
上面的代码会删除标点符号,urls,把test放在小写,提取用户名作为例子。当我运行该代码时,它给出了一个错误。
TypeErrorTraceback (most recent call last)
<ipython-input-3-8254e078073a> in <module>()
5 for i in range(len(ctweet['Tweets'])):
6 try:
----> 7 ctweet['tweetos'][i] = ctweet['Tweets'].str.split(' ')[i][0]
8 except AttributeError:
9 ctweet['tweetos'][i] = 'other'
TypeError: 'float' object has no attribute '__getitem__'
这个错误是什么意思?我怎么解决这个问题。我正在使用 Jupyter Notebook 5.4.1
更新部分
AttributeErrorTraceback (most recent call last)
<ipython-input-7-bb6b24f62739> in <module>()
16 # remove URLs, RTs, and twitter handles
17 for i in range(len(ctweet['Tweets'])):
---> 18 ctweet['Tweets'][i] = " ".join([word for word in ctweet['Tweets'][i].split()
19 if 'http' not in word and '@' not in word and '<' not in word])
20
AttributeError: 'float' object has no attribute 'split'
【问题讨论】:
-
我认为
ctweet['Tweets'].str.split(' ')[i][0]应该像ctweet['Tweets'][i].str.split(' ')[0] -
-Hamza Haider,我做到了。以为代码会正确运行,但在第 18 行出现另一个错误 ' ctweet['Tweets'][i] = " ".join([word for word in ctweet['Tweets'][i].split() ' " AttributeError: 'float' 对象没有属性 'split'"
-
好吧,看起来
ctweet['Tweets'][i]并不总是字符串,在这种情况下它是一个浮点数。你能确认ctweet['Tweets']中的所有项目都是字符串吗? -
“推文”列中的每个值都不是字符串。
-
哈姆扎·海德尔。我得到了代码工作。我必须将值转换为字符串。感谢您的帮助。
标签: python pandas nlp jupyter-notebook data-cleaning