如何将字典的键转换为一栏式的熊猫数据框?

等等

我想获取键(而不是值),并从这些键创建一个单列的数据框。

我目前有:

dictionary = {"Eva": "Person", "Xior": "Building", "Potatoes": "Food", "Dog": "Animal"} 

## my current code is: 
df1 = pd.DataFrame.from_dict(list(dictionary.items())) 
df1

预期的输出:

       Word
0      Xior
1       Dog
2       Eva
3  Potatoes

目前,我只能以pandas df的形式获得完整的词典(键,值),而不仅仅是键。我还必须先将字典转换成列表,否则会出现ValueError。

安塞夫

采用:

pd.DataFrame(list(dictionary),columns=['word'])

要么

pd.Series(list(dictionary)).to_frame('word')

输出:

       word
0       Eva
1      Xior
2  Potatoes
3       Dog

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章