Skip to main content
 首页 » 编程设计

python之循环问题的 Twitter 数据框架

2025年05月04日25xiaohuochai

我正在尝试为数据框中的单个列遍历 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.' 

如何解决此错误?

请您参考如下方法:

我觉得

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

实际上应该是

sentiment_value, confidence = s.sentiment(t) 

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