python - 无法将我的字典转换为数据框
问题描述
我有一本这样的字典
{'community_0': 30,
'community_1': 29,
'community_2': 15,
'community_3': 16,
'community_4': 123,
'community_5': 9,
'community_6': 36,
'community_7': 71,
'community_8': 95,
'community_9': 21}
我想将其转换为熊猫数据框。我试过pd.DataFrame.from_dict(dict, orient='index')
了,但它给了我别的东西:
我参考了这篇文章Convert Python dict into a dataframe 但它并没有太大帮助。
任何建议,将不胜感激。
解决方案
可以直接从字典中创建 a pd.Series
,然后使用该.to_frame()
方法将 apd.Series
转换为单列 DataFrame:
import pandas as pd
d = {'community_0': 30,
'community_1': 29,
'community_2': 15,
'community_3': 16,
'community_4': 123,
'community_5': 9,
'community_6': 36,
'community_7': 71,
'community_8': 95,
'community_9': 21}
pd.Series(d).to_frame()
返回:
0
community_0 30
community_1 29
community_2 15
community_3 16
community_4 123
community_5 9
community_6 36
community_7 71
community_8 95
community_9 21
推荐阅读
- java - 在 Maven 标记中添加两个标志到 SkipTests
- mysql - 尝试配置 CartoDB 时无法使用 db:create
- c# - 使用 C# 删除 Windows 用户(远程)
- javascript - 使用另一个 json 对象从 json 对象中删除特定内容
- three.js - 使用 InstancedBufferGeometry 时未定义索引
- javascript - 如何让particle.js 只移动一个特定的div?
- html - CSS:计算两个重叠矩形在旋转后的溢出
- c++ - 我可以安全地混合使用 -std=c++11 和 -std=c++14 编译的库吗?
- cmake - CMake 将项目文件夹移动到另一台计算机
- php - 在模型 PHP CI 中获取结果搜索