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?
defaultdictto collate ur data and read once into a dataframe. that reduces ur for loops significantly