Python将具有多个键的Dict转换为Dataframe

标签 python python-3.x pandas dictionary dataframe

我有一个从网络数据创建的字典。我从一个空的 Dict 开始,例如 d = {}。然后我遍历每个网页并将每一页数据存储在 d[i] 中,其中 i 从 0 开始,i = 0。然后我有一个包含多个键/索引的字典(我不确定正确的术语是什么)。我可以输入 d[0] 查看第一页,输入 d[1] 查看第二页,依此类推。我正在尝试合并这些键/索引中的每一个,以便我可以将此 Dict 转换为 Dataframe。 Dict 看起来像这样:

{0:    Age (Days) Number of Items in Queue Number of Items in WIP  \
 3           4                        0                      2
 4           5                        0                      1
 5           6                        0                      2
 6           7                        0                      4
 7           9                        0                      1
 8          12                        0                      2
 9          14                        0                      4
 10         15                        0                      3
 11         17                        0                      1
 12         19                        0                      2
1:    Age (Days) Number of Items in Queue Number of Items in WIP  \
 3           0                        0                      1
 4           1                        0                      8
 5           2                        0                      3
 6           3                        0                      6
 7           4                        0                      1
 8           5                        0                      7
 9           9                        0                      9
 10         10                        0                      4
 11         11                        0                      3
 12         12                        0                      8
 13         13                        0                      2
 14         14                        0                      1
 15         15                        0                      5
 16         16                        0                      6
 17         17                        0                      1
 18         18                        0                      5
 19         19                        0                      7
 20         20                        0                      2}

我不能简单地使用 pd.DataFrame(d)。我尝试使用 pd.DataFrame.from_dict(list(d.values())) 但这没有给出我期望的输出。我使用 pd.DataFrame.from_dict(d, orient='index') 得到相同的输出。不需要的输出只是列出每个键/索引,如下所示:

In [102]: pd.DataFrame.from_dict(d, orient='index')
Out[102]:
                                                   0
0     Age (Days) Number of Items in Queue Number ...
1     Age (Days) Number of Items in Queue Number ...
2     Age (Days) Number of Items in Queue Number ...

我希望将 d[1] 中的行添加到 d[0] 并保留相同的列名。

最佳答案

IIUC 你可以 concatenate他们这样:

df = pd.concat(d.values(), ignore_index=True)

关于Python将具有多个键的Dict转换为Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452808/

相关文章:

python - 适用于 Python 的 LinkedIn API

python - 保持文件句柄打开的缺点?

python - 连接 pandas DataFrames 只保留列中具有匹配值的行?

python - 如何运行kivy程序?

python - python 3 中最快的标准输入/输出 IO?

Python:游程编码

python - 如何在 Python 中的进程之间传递堆栈跟踪?

python - 在类中存储集合信息

python - 从 Pandas 列中分解 dict

python - 如何用 python 绘制 x y 线图?