[英]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.得到一个字典,其中'neg'是一列,'neu'是一列,'pos'是一列,以字符为索引。
I am able to do it by extracting each to lists with for loops and then those lists to series我可以通过将每个列表提取到带有 for 循环的列表然后将这些列表提取到系列来做到这一点
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?但是有没有更简单的方法呢?
You just need to transpose the df with.T to access the transpose() method.您只需要用.T 转置 df 即可访问 transpose() 方法。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.