Twitter数据框架循环问题

时间:2015-09-18 20:31:42

标签: python for-loop pandas

我正试图在数据框中为单个列循环10K行,试图对Twitter数据进行情绪分析:

twitter_data = pd.DataFrame(pd.read_csv("/Users/Stu/Jupyter/Sentiment Score Test 4.csv", encoding = "latin-1"))

screenName = twitter_data.screenName
tweet = twitter_data.text
favorites = twitter_data.favoriteCount
retweets = twitter_data.favoriteCount

tweet.head()

0    Finally finished the first season of Mr. Robot...
1              @DavidVonderhaar thoughts on Mr. Robot?
2    I liked a @YouTube video from @seasonedreviews...
3    Am I sane enough not to tweet about Mr Robot f...
4    the guy from queer as folk is in mr robot omg ...
Name: text, dtype: object

然后我会从我创建的模块(通过tutes的帮助)中使用我的情感函数打印出我想要的值:

print(tweet[0])
sentiment_value, confidence = s.sentiment(tweet[0])
print(sentiment_value)
print(confidence)

Mr. Robot had a perfect season on Rotten Tomatoes⦠http://t.co/CPsZUid5knââ¦â¦â¦â¦ http://t.co/AVGzjA7KGKââ¦â¦â¦â¦â¦â¦â¦ http://t.co/2iB0vX0mva
pos
1.0

但是,当我尝试通过for循环推送此数据以通过执行以下操作打印出情绪值时:

for t in tweet:
    sentiment_value, confidence(s.sentiment(tweet[t]))
    print(sentiment_value)

我被抛出以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()

pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7200)()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-113-9166c7972a3d> in <module>()
     14 
     15 for t in tweet:
---> 16     sentiment_value, confidence(s.sentiment(tweet[t]))
     17     print(sentiment_value)
     18 

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/series.py in __getitem__(self, key)
    519     def __getitem__(self, key):
    520         try:
--> 521             result = self.index.get_value(self, key)
    522 
    523             if not np.isscalar(result):

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/index.py in get_value(self, series, key)
   1593 
   1594         try:
-> 1595             return self._engine.get_value(s, k)
   1596         except KeyError as e1:
   1597             if len(self) > 0 and self.inferred_type in ['integer','boolean']:

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3113)()

pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2844)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3761)()

KeyError: 'Finally finished the first season of Mr. Robot.Pure, fucked up genius, love everything about this twisted show.'

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

我认为

sentiment_value, confidence(s.sentiment(tweet[t]))

实际应该是

sentiment_value, confidence = s.sentiment(t)

请记住,循环正在为你做索引。