简体   繁体   English

如何将具有字典值的字典转换为 pandas DataFrame 并将这些值的键作为列

[英]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.

相关问题 如果值是列表,如何根据值将字典键转换为 dataframe 列 - How to transform dictionary keys into a dataframe column based on the values if the values are lists Python:从pandas数据帧生成字典,行作为键,列作为值 - Python: Generate dictionary from pandas dataframe with rows as keys and columns as values Pandas:将字典转换为 dataframe,其中键和值是列 - Pandas: Convert dictionary to dataframe where keys and values are the columns 有效地将字典的键和值转换为 pandas dataframe 中的列 - Converting both a dictionary's keys and values to columns in a pandas dataframe efficiently Dataframe 到字典分组和键/值是其他列 - Dataframe to Dictionary Grouped and Keys/Values are Other Columns 如何使用字典键和值重命名Pandas DataFrame中的列? - How do I use dictionary keys and values to rename columns in a pandas DataFrame? DataFrame 列到具有多个键和值的字典 - DataFrame columns to dictionary with multiple keys and values 如何将字典的键映射到 Pandas Dataframe 的列值 - how to map keys of the dictionary to a column values of pandas Dataframe 如何根据字典键和值过滤熊猫数据框行? - How to filter pandas dataframe rows based on dictionary keys and values? 如何将 pandas DataFrame 压缩成具有唯一键和列出值的字典? - How to compress pandas DataFrame into a dictionary with unique keys and listed values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM