newtopython
newtopython

Reputation: 11

Converting value of a dictionary of lists to floats

Newbie to Python here! I could really do with some help on a sentiment project I'm doing in Python.

I've build this dictionary which I'd like to use to go through comments I've scraped from a Guardian article. I want to add an extra column to the guardian_comments dataframe which signifies whether the value in the sentiment_score column is very_negative, negative, neutral, positive or very_positive:

lookup_dict = {
"very_negative":[-0.9, -0.8, -0.7, -0.6], 
"negative":[-0.5, -0.4, -0.3, -0.2, -0.1],
"neutral":[0],
"positive":[0.1, 0.2, 0.3, 0.4, 0.5], 
 "very_positive":[0.6, 0.7, 0.8, 0.9, 1]}

guardian_comments['sentiment_level'] = guardian_comments['sentiment_score'].map(lambda x: lookup_dict[x])

However, I'm receiving a KeyError which I think is down to the fact the dictionary lists haven't been converted to floats. Can anyone advise me on how to do this?

Thank you!

Upvotes: 0

Views: 54

Answers (2)

pakpe
pakpe

Reputation: 5479

If you wish to maintain the current structure of your dictionary, the least confusing way to do this is to define a helper function that you can apply as lambda to your dictionary:

def find_level(row):
    for key, value in lookup_dict.items():
        if row['sentiment_score'] in value:
            return key

df['sentiment_level'] = df.apply(lambda row: find_level(row), axis = 1)

#output:
   sentiment_score sentiment_level
0             -0.5        negative
1              0.5        positive
2              0.6   very_positive
3             -0.2        negative
4             -0.1        negative

Upvotes: 1

wuerfelfreak
wuerfelfreak

Reputation: 2439

lookup_dict[x] retrieves the entry in lookup_dict with key x.
e.g. lookup_dict["very_negative"] would return [-0.9, -0.8, -0.7, -0.6] But lookup_dict[-0.6] will result in a KeyError.
So your lambda function does not what you think it does.

The simplest solution in my opinion would be like this:

lookup_dict = {
    -0.9:"very_negative",
    -0.8:"very_negative",
    -0.7:"very_negative",
    -0.6:"very_negative",
    -0.5:"negative",
    -0.4:"negative",
    -0.3:"negative",
    -0.2:"negative",
    -0.1:"negative",
    0:"neutral",
    0.1:"positive",
    0.2:"positive",
    0.3:"positive",
    0.4:"positive",
    0.5:"positive",
    0.6:"very_positive",
    0.7:"very_positive",
    0.8:"very_positive",
    0.9:"very_positive",
    1:"very_positive"
}

guardian_comments['sentiment_level'] = guardian_comments['sentiment_score'].map(lambda x: lookup_dict[x])

Upvotes: 1

Related Questions