python-3.x - I want to convert a dictionary to pandas dataFrame
问题描述
di={'ind': 1, 'age': 59, 'bp': 70, 'sg': 1.01, 'al': 1.0, 'su': 3.0, 'rbc': 0.0, 'pc': 0.0, 'pcc': 0.0, 'ba': 0.0, 'bgr': 424.0, 'bu': 55.0, 'sc': 1.7, 'sod': 138.0, 'pot': 4.5, 'hemo': 12.0, 'pcv': 37.0, 'wbcc': 10200.0, 'rbcc': 4.1, 'htn': 1.0, 'dm': 1.0, 'cad': 1.0, 'appet': 1.0, 'pe': 0.0, 'ane': 1.0}
I'm having this dictionary, and I want to convert this to a pandas dataframe with 'ind': 1
as the index, 24 columns and 1 row.
These are the names of each column that I want to have in my df:-
d=['age', 'bp', 'sg','al', 'su', 'rbc', 'pc', 'pcc', 'ba', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'pcv', 'wbcc', 'rbcc', 'htn', 'dm','cad', 'appet', 'pe', 'ane']
Please guide me with it. I tried the method pd.DataFrame(di.items(), columns=d)
but it returned a df with 1 column and 24 rows, I wan the reciprocal of it i.e. 24 columns and 1 row.
Thank You
解决方案
You can try
df = pd.Series(di).to_frame(0).T.set_index('ind')
推荐阅读
- python-3.x - 有没有办法根据现有 Excel 工作表上的数据创建迷你图?
- html - CSS 和页面大小调整
- python - Python 和 Excel 中某些矩阵的逆矩阵是不同的。我应该考虑哪些结果?
- python - 将文本列表分配给变量 python
- sql - SQL电子商务数据库。如何在同一行关联外键?
- dig - 解码挖掘输出:+norecurse @nameserver MX 到域
- windows - Windows 10 上的 Apache 2.4.46 虚拟主机
- c# - Blazor EditForm 以编程方式提交
- spring-boot - 在哪里可以找到 SpringBoot 包的 SHA256/SHA512 哈希?
- javascript - 如何将嵌入查询与 wordpress rest api 一起使用?