sds2019 icon indicating copy to clipboard operation
sds2019 copied to clipboard

Working With Lists In DataFrames

Open IAmAndreasSK opened this issue 6 years ago • 1 comments

Hi,

My group and I use the hashtags #MakeAmericaGreatAgain and #ImWithHer as a basis for our project. However, we would also like to see the other hashtags which the tweets have been using. To that end, we wrote a code that would insert a list of all hashtags used under a "All Hashtags" column for all tweets.

hu=[]
for i in range(len(data["results"])):
    ho=[]
    for d in data['results'][i]['entities']['hashtags']:
        ho.append(str(d["text"]))
    hu.append(ho)
        
df["Hashtag"].copy()[0]=hu[0]

We were wondering the following:

  1. How can we easily count the number of times the different hashtags have been used? We tried df["Hashtag"].value_counts() but that counts the number of times specific lists occur rather than the elements in them. I guess we could do a loop but I'd hope for a more elegant solution.

  2. Is there a way to write the general code in a more 'smooth' way? And should we even use lists in the way we have done?

Thank you!

IAmAndreasSK avatar Aug 24 '19 18:08 IAmAndreasSK

I personally would use a dictionary. See this: https://stackoverflow.com/questions/3496518/python-using-a-dictionary-to-count-the-items-in-a-list

Or, as posted in the link, use a Counter funciton: https://docs.python.org/2/library/collections.html#collections.Counter

jrkkfst avatar Aug 26 '19 06:08 jrkkfst