Ben Smith
Ben Smith

Reputation: 380

How to transform a dictionary with dictionary values into pandas DataFrame with those values' keys as columns

How would a take a dictionary like:

{'Character1': {'neg': 0.089, 'neu': 0.768, 'pos': 0.143, 'compound': 1.0},
 'Character2': {'neg': 0.095, 'neu': 0.776, 'pos': 0.129, 'compound': 1.0},
 'Character3': {'neg': 0.084, 'neu': 0.807, 'pos': 0.11, 'compound': 1.0},
 'Character4': {'neg': 0.077, 'neu': 0.799, 'pos': 0.124, 'compound': 1.0},
 'Character5': {'neg': 0.118, 'neu': 0.764, 'pos': 0.118, 'compound': -0.9991},
 'Character6': {'neg': 0.1, 'neu': 0.776, 'pos': 0.123, 'compound': 1.0},
 'Character7': {'neg': 0.102, 'neu': 0.744, 'pos': 0.154, 'compound': 1.0},
 'Character8': {'neg': 0.078, 'neu': 0.798, 'pos': 0.124, 'compound': 1.0},
 'Character9': {'neg': 0.131, 'neu': 0.704, 'pos': 0.165, 'compound': 0.9999},
 'Character10': {'neg': 0.082, 'neu': 0.773, 'pos': 0.145, 'compound': 0.9999}}

to get a dictionary where 'neg' is a column, 'neu' is a column and 'pos' is a column, with the Characters as the index.

I am able to do it by extracting each to lists with for loops and then those lists to series

chars = list(sentiments.keys())
negs = []
for val in sentiments.values():
    for k, v in val.items():
        if k == 'neg':
            negs.append(v)

neuts = []
for val in sentiments.values():
    for k, v in val.items():
        if k == 'neu':
            neuts.append(v)

poss = []
for val in sentiments.values():
    for k, v in val.items():
        if k == 'pos':
            poss.append(v)

d = {"Neg. Score": negs, "Neu. Score": neuts, "Pos. Score": poss}
sentiments_df = pd.DataFrame(data=d, index=char_series)

But is there an easier way to do it?

Upvotes: 1

Views: 73

Answers (1)

Matthew Borish
Matthew Borish

Reputation: 3086

You just need to transpose the df with .T to access the transpose() method.

the_dict = {'Character1': {'neg': 0.089, 'neu': 0.768, 'pos': 0.143, 'compound': 1.0},
 'Character2': {'neg': 0.095, 'neu': 0.776, 'pos': 0.129, 'compound': 1.0},
 'Character3': {'neg': 0.084, 'neu': 0.807, 'pos': 0.11, 'compound': 1.0},
 'Character4': {'neg': 0.077, 'neu': 0.799, 'pos': 0.124, 'compound': 1.0},
 'Character5': {'neg': 0.118, 'neu': 0.764, 'pos': 0.118, 'compound': -0.9991},
 'Character6': {'neg': 0.1, 'neu': 0.776, 'pos': 0.123, 'compound': 1.0},
 'Character7': {'neg': 0.102, 'neu': 0.744, 'pos': 0.154, 'compound': 1.0},
 'Character8': {'neg': 0.078, 'neu': 0.798, 'pos': 0.124, 'compound': 1.0},
 'Character9': {'neg': 0.131, 'neu': 0.704, 'pos': 0.165, 'compound': 0.9999},
 'Character10': {'neg': 0.082, 'neu': 0.773, 'pos': 0.145, 'compound': 0.9999}}

df = pd.DataFrame(the_dict).T

print(df)

    neg neu pos compound
Character1  0.089   0.768   0.143   1.0000
Character2  0.095   0.776   0.129   1.0000
Character3  0.084   0.807   0.110   1.0000
Character4  0.077   0.799   0.124   1.0000
Character5  0.118   0.764   0.118   -0.9991
Character6  0.100   0.776   0.123   1.0000
Character7  0.102   0.744   0.154   1.0000
Character8  0.078   0.798   0.124   1.0000
Character9  0.131   0.704   0.165   0.9999
Character10 0.082   0.773   0.145   0.9999

Upvotes: 4

Related Questions