python - 以表格格式打印嵌套字典键和值

标签 python pandas dictionary nested

我想将我的嵌套字典打印成表格格式,看起来像这样:

**ID**      **Count**    **Time**   **OutCount**  **OutTime**

  262          725        15:29          725         15:29

等等......

我的嵌套字典是:

{'262': {'count': 725,'time': '15:29','outcount': 725,'outtime': '15:29'},
'343': {'count': 699,'time': '15:29','outcount': 699,'outtime': '15:29'},
'809': {'count': 695,'time': '15:29','outcount': 695,'outtime': '15:29'}

最佳答案

使用pd.DataFrame.from_dict

pd.DataFrame.from_dict(d, orient='index')
     count   time  outcount outtime
262    725  15:29       725   15:29
343    699  15:29       699   15:29
809    695  15:29       695   15:29

或使用 reset_index

pd.DataFrame.from_dict(d, orient='index').reset_index()
  index  count   time  outcount outtime
0   262    725  15:29       725   15:29
1   343    699  15:29       699   15:29
2   809    695  15:29       695   15:29

关于python - 以表格格式打印嵌套字典键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61745024/

相关文章:

python - python 循环中的 lambda 运算符

python - KeyError: 0 在字典中遍历字典的键时

python - 什么时候在 Python 中引发 KeyboardInterrupt?

python - 如何生成仅包含 1's and 0' s 的 14x10 矩阵的所有可能组合

python - 匹配以两位或三位数字开头但不包含某处特定模式的事件

python - 在聊天信使中连接到 redis-server 时出错

python - 如何使用 Groupby 函数查找数据框中的最高值

python - DataFrame MultiIndex - 按值查找列

python - 将 dict 转换为嵌套列表,将键插入每个子列表

pandas - 为什么在绘制数据透视表时,图例标题中会出现 'None'?