简体   繁体   English

Dataframe 来自字典列表的字典?

[英]Dataframe from a dict of lists of dicts?

I have a dict of lists of dicts.我有一个字典列表的字典。 What is the most efficient way to convert this into a DataFrame in pandas?将其转换为DataFrame中的 DataFrame 的最有效方法是什么?

data = {
     "0a2":[{"a":1,"b":1},{"a":1,"b":1,"c":1},{"a":1,"b":1}],
     "279":[{"a":1,"b":1,"c":1},{"a":1,"b":1,"d":1}],
     "ae2":[{"a":1,"b":1},{"a":1,"d":1},{"a":1,"b":1},{"a":1,"d":1}], 
     #...
}
import pandas as pd
pd.DataFrame(data, columns=["a","b","c","d"])

数据

What I've tried:我试过的:

One solution is to denormalize the data like this, by duplicating the "id" keys:一种解决方案是通过复制“id”键来像这样对数据进行非规范化:

bad_data = [
      {"a":1,"b":1,"id":"0a2"},{"a":1,"b":1,"c":1,"id":"0a2"},{"a":1,"b":1,"id":"0a2"},
      {"a":1,"b":1,"c":1,"id":"279"},{"a":1,"b":1,"d":1,"id":"279"},
      {"a":1,"b":1,"id":"ae2"},{"a":1,"d":1,"id":"ae2"},{"a":1,"b":1,"id":"ae2"},{"a":1,"d":1,"id":"ae2"}
]
pd.DataFrame(bad_data, columns=["a","b","c","d","id"])

But my data is very large, so I'd prefer some other hierarchical index solution.但是我的数据非常大,所以我更喜欢其他一些分层索引解决方案。

IIUC, you can do (remcomended) IIUC,你可以做(推荐)

new_df = pd.concat((pd.DataFrame(d) for d in data.values()), keys=data.keys())

Output: Output:

       a    b    c    d
0a2 0  1  1.0  NaN  NaN
    1  1  1.0  1.0  NaN
    2  1  1.0  NaN  NaN
279 0  1  1.0  1.0  NaN
    1  1  1.0  NaN  1.0
ae2 0  1  1.0  NaN  NaN
    1  1  NaN  NaN  1.0
    2  1  1.0  NaN  NaN
    3  1  NaN  NaN  1.0

Or或者

pd.concat(pd.DataFrame(v).assign(ID=k) for k,v in data.items())

Output: Output:

   a    b    c   ID    d
0  1  1.0  NaN  0a2  NaN
1  1  1.0  1.0  0a2  NaN
2  1  1.0  NaN  0a2  NaN
0  1  1.0  1.0  279  NaN
1  1  1.0  NaN  279  1.0
0  1  1.0  NaN  ae2  NaN
1  1  NaN  NaN  ae2  1.0
2  1  1.0  NaN  ae2  NaN
3  1  NaN  NaN  ae2  1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM