Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将具有字典值的字典转换为具有这些值的数据帧';键作为列_Python_Pandas_Dictionary - Fatal编程技术网

Python 如何将具有字典值的字典转换为具有这些值的数据帧';键作为列

Python 如何将具有字典值的字典转换为具有这些值的数据帧';键作为列,python,pandas,dictionary,Python,Pandas,Dictionary,一本字典会是什么样子的: {'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}, 'Characte

一本字典会是什么样子的:

{'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}}
要获取一个字典,其中“neg”是一列,“neu”是一列,“pos”是一列,以字符作为索引

我可以通过使用for循环提取每个to列表,然后将这些列表提取到系列来实现这一点

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)

但是有更简单的方法吗?

您只需要将df与.T进行转置就可以访问transpose()方法


您只需要将df与.T转置即可访问transpose()方法


你可以使用一个
defaultdict
来整理你的数据,并在数据框中读取一次。这大大减少了ur for循环。您可以使用
defaultdict
来整理您的数据并将其读入数据帧中一次。这大大降低了循环的ur
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