Reputation: 1411
I am getting an attribute error while running this program on Windows command pan.I have json data which I got using Tweepy
import json
import pandas as pd
import matplotlib.pyplot as plt
import re
tweets_data_path = 'C:/Users/e2sn7cy/Documents/GitHub/twitter_data.txt'
tweets_data = []
tweets_file = open(tweets_data_path, 'r')
for line in tweets_file:
try:
tweet = json.loads(line)
except:
continue
if not all([x in tweet for x in ['text', 'lang', 'place']]):
continue
if tweet['place'] and not 'country' in tweet['place']:
continue
tweets_data.append(tweet)
#print len(tweets_data)
#DataFrame
tweets = pd.DataFrame()
#adding columns
tweets['text'] = map(lambda tweet:tweet['text'] if tweet['text'] else '', tweets_data)
#tweets['text'] = [tweet['text'] for tweet in tweets_data]
tweets['lang'] = map(lambda tweet:tweet['lang'] if tweet['lang'] else '', tweets_data)
#tweets['lang'] = [tweet['lang'] for tweet in tweets_data]
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)
#Adding Charts
tweets_by_lang = tweets['lang'].value_count()
#pd.value_counts(tweets.values.flatten())
fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang[:5].plot(ax=ax, kind='bar', color='red')
Attribute Error:
(venv) c:\Users\e2sn7cy\Documents\GitHub\Twitter-App>python twitter_analytics.py
Traceback (most recent call last):
File "twitter_analytics.py", line 41, in <module>
tweets_by_lang = tweets['lang'].value_count()
File "C:\myPython\venv\lib\site-packages\pandas\core\generic.py", line 2083, in __getattr__
(type(self).__name__, name))
AttributeError: 'Series' object has no attribute 'value_count'
Here is Twitter Data. There is more than 90k lines, I am posting only 2.
{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574643897778176,"id_str":"608574643897778176","text":"D\u00e9couvrez le TOP 10 des plus beaux buts de la Ligue des Champions ! #LDC \n\nVid\u00e9o > http:\/\/t.co\/qrOS6JGBYa http:\/\/t.co\/gSBzkhpdWQ","source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":18802382,"id_str":"18802382","name":"Football.fr","screen_name":"football_fr","location":"","url":"http:\/\/www.football.fr","description":"Compte Twitter officiel de Football.fr, site d'actualit\u00e9s de Newsweb Lagard\u00e8re Active.","protected":false,"verified":true,"followers_count":15012,"friends_count":1290,"listed_count":165,"favourites_count":30,"statuses_count":19099,"created_at":"Fri Jan 09 14:35:24 +0000 2009","utc_offset":7200,"time_zone":"Paris","geo_enabled":true,"lang":"fr","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/403703872\/h_h_h_h_.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/403703872\/h_h_h_h_.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/562565984548970496\/ggwgwYbh_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/562565984548970496\/ggwgwYbh_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/18802382\/1433626125","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"LDC","indices":[68,72]}],"trends":[],"urls":[{"url":"http:\/\/t.co\/qrOS6JGBYa","expanded_url":"http:\/\/www.football.fr\/ligue-des-champions\/articles\/ronaldo-messi-benzema-les-10-plus-beaux-buts-de-la-saison-video-668464\/","display_url":"football.fr\/ligue-des-cham\u2026","indices":[86,108]}],"user_mentions":[],"symbols":[],"media":[{"id":608574518903341056,"id_str":"608574518903341056","indices":[109,131],"media_url":"http:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","url":"http:\/\/t.co\/gSBzkhpdWQ","display_url":"pic.twitter.com\/gSBzkhpdWQ","expanded_url":"http:\/\/twitter.com\/football_fr\/status\/608574643897778176\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":226,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":400,"resize":"fit"},"large":{"w":900,"h":600,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":608574518903341056,"id_str":"608574518903341056","indices":[109,131],"media_url":"http:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/CHIXTPKWgAAqSXS.jpg","url":"http:\/\/t.co\/gSBzkhpdWQ","display_url":"pic.twitter.com\/gSBzkhpdWQ","expanded_url":"http:\/\/twitter.com\/football_fr\/status\/608574643897778176\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":226,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":400,"resize":"fit"},"large":{"w":900,"h":600,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"fr","timestamp_ms":"1433930476532"}
{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574645676204032,"id_str":"608574645676204032","text":"#Coach Training Manager opportunity with Lawn #Tennis Association http:\/\/t.co\/eLUBYQVJZj","source":"\u003ca href=\"http:\/\/www.hootsuite.com\" rel=\"nofollow\"\u003eHootsuite\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":191782743,"id_str":"191782743","name":"Jump in Sport","screen_name":"jumpinsport","location":"Lausanne, Switzerland ","url":"http:\/\/www.jumpinsport.com\/","description":"We collect the best sports related jobs from top sources around the world and show them all in one place! Follow us for great job opportunities.","protected":false,"verified":false,"followers_count":8279,"friends_count":882,"listed_count":168,"favourites_count":38,"statuses_count":30202,"created_at":"Fri Sep 17 10:18:02 +0000 2010","utc_offset":7200,"time_zone":"Bern","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"DBE9ED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/215611005\/logo_jump_3_square__4_.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/215611005\/logo_jump_3_square__4_.jpeg","profile_background_tile":false,"profile_link_color":"CC3366","profile_sidebar_border_color":"DBE9ED","profile_sidebar_fill_color":"E6F6F9","profile_text_color":"333333","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1267864780\/logo_jump_3_square__4__normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1267864780\/logo_jump_3_square__4__normal.jpeg","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Coach","indices":[0,6]},{"text":"Tennis","indices":[46,53]}],"trends":[],"urls":[{"url":"http:\/\/t.co\/eLUBYQVJZj","expanded_url":"http:\/\/www.jumpinsport.com\/?q=node\/30549","display_url":"jumpinsport.com\/?q=node\/30549","indices":[66,88]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1433930476956"}
{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574645772648449,"id_str":"608574645772648449","text":"McCartney needs to restore the passion and belief back into #newcastleunited, needs the fans behind him #football #PremierLeague","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":3292127782,"id_str":"3292127782","name":"Lonely Corner Flag","screen_name":"TLCFblog","location":"","url":"http:\/\/thelonelycornerflag.blogspot.co.uk\/","description":"People have kicked me, danced around me, even hit me with wayward shots, but unlike other corner flags who are left in the cold, this one has a voice!","protected":false,"verified":false,"followers_count":29,"friends_count":68,"listed_count":7,"favourites_count":1,"statuses_count":86,"created_at":"Wed May 20 23:12:00 +0000 2015","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"3B94D9","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/601165229297430528\/SXqZdVPC_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/601165229297430528\/SXqZdVPC_normal.jpg","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"newcastleunited","indices":[60,76]},{"text":"football","indices":[104,113]},{"text":"PremierLeague","indices":[114,128]}],"trends":[],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1433930476979"}
{"created_at":"Wed Jun 10 10:01:16 +0000 2015","id":608574645818798080,"id_str":"608574645818798080","text":"\u041c\u0430\u0433\u043e\u043c\u0435\u0434 \u041e\u0437\u0434\u043e\u0435\u0432: \"\u0422\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0432 \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u043e\u043c \u043a\u043b\u044e\u0447\u0435\".\nhttp:\/\/t.co\/4ZXaBShx4k\n#\u0424\u0443\u0442\u0431\u043e\u043b #\u041d\u043e\u0432\u043e\u0441\u0442\u0438","source":"\u003ca href=\"http:\/\/www.youtube.com\/\" rel=\"nofollow\"\u003eBestNewsSoccer\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1484814313,"id_str":"1484814313","name":"\u041d\u043e\u0432\u043e\u0441\u0442\u0438 \u0424\u0443\u0442\u0431\u043e\u043b\u0430","screen_name":"BestNewsSoccer","location":"","url":"http:\/\/vk.com\/bestnewssoccer","description":"\u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0432\u0435\u0436\u0438\u0445 \u0444\u0443\u0442\u0431\u043e\u043b\u044c\u043d\u044b\u0445 \u043d\u043e\u0432\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f \u0432\u0430\u0441.\n\u0413\u0440\u0443\u043f\u043f\u0430 Vkontakte, \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0438\u0434\u0435\u043e \u0438 \u0444\u043e\u0442\u043e http:\/\/t.co\/7PlwxpQJ3s Football Mems https:\/\/t.co\/ZWBbVjcrXO","protected":false,"verified":false,"followers_count":4603,"friends_count":0,"listed_count":46,"favourites_count":0,"statuses_count":612979,"created_at":"Wed Jun 05 11:36:05 +0000 2013","utc_offset":10800,"time_zone":"Baghdad","geo_enabled":false,"lang":"ru","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000120574093\/d08a1e24f292c31aa98fe0a654b499da.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000120574093\/d08a1e24f292c31aa98fe0a654b499da.jpeg","profile_background_tile":true,"profile_link_color":"3B94D9","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/465545718740967424\/2QxPHFgP_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/465545718740967424\/2QxPHFgP_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1484814313\/1399322008","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"\u0424\u0443\u0442\u0431\u043e\u043b","indices":[81,88]},{"text":"\u041d\u043e\u0432\u043e\u0441\u0442\u0438","indices":[89,97]}],"trends":[],"urls":[{"url":"http:\/\/t.co\/4ZXaBShx4k","expanded_url":"http:\/\/football.kulichki.net\/rusnews\/news.htm?353697","display_url":"football.kulichki.net\/rusnews\/news.h\u2026","indices":[58,80]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"ru","timestamp_ms":"1433930476990"}
Upvotes: 2
Views: 194
Reputation: 394101
It's value_counts
not value_count
.
Change to this:
tweets_by_lang = tweets['lang'].value_counts()
Upvotes: 2