将字典转换为两栏熊猫数据框

时间:2020-05-21 13:31:02

标签: python pandas dataframe dictionary

我在python中有一个名为word_counts的字典,它由关键字和值组成,这些关键字和值表示它们在给定文本中出现的频率:

word_counts = {'the':2, 'cat':2, 'sat':1, 'with':1, 'other':1}

我现在需要将其放入具有两列的pandas DataFrame中:名为“ word”的列表示单词,而名为“ count”的列表示频率。

3 个答案:

答案 0 :(得分:0)

>>> import pandas as pd
>>> pd.DataFrame(list(word_counts.items()), columns=['word', 'count'])
    word  count
0    the      2
1    cat      2
2    sat      1
3   with      1
4  other      1

答案 1 :(得分:0)

您可以从字典中创建数据框:

df=pd.DataFrame({"word":list(word_counts.keys()) , "count": list(word_counts.values())})

答案 2 :(得分:0)

您可以使用.from_dict的{​​{1}}附件

pd.DataFrame