[英]create df from list of dictionary of dictionaries
Trying create dataframe from what I believe is referred to as a list of dictionary of dictionaries.尝试从我认为被称为词典列表的内容中创建 dataframe 。
list of dictionary of dictionaries:词典列表:
test_dict = {'name': 'Henry VIII', 'wives': [{'WED': '11JUNE1509', 'spouse': 'TBC', 'marriage': {'name': 'Henry VIII', 'identifier': 'henry'}, 'died': 0.0, 'marriage_lasted': {'yrs': 23.0, 'mth': 11.0, 'days': 12.0}, 'timestamp': '2022-07-30T13:48:44+00:00', 'last_at': '2022-07-30T13:48:44+00:00', 'fetch_at': '2022-07-30T13:51:47+00:00', 'url': 'https://https://en.wikipedia.org/wiki/Wives_of_Henry_VIII', 'husband': 'king henry of england', 'wife': 'catherine of aragon'}, {'WED': '28MAY1533', 'spouse': 'TBC', 'marriage': {'name': 'Henry VIII', 'identifier': 'henry'}, 'died' : 1.0, 'marriage_lasted': {'yrs': 2.0, 'mth': 11.0, 'days': 19.0}, 'timestamp': '2022-07-30T13:51:47+00:00', 'last_at': '2022-07-30T13:51:47+00:00', 'fetch_at': '2022-07-30T13:51:47+00:00', 'url': 'https://https://en.wikipedia.org/wiki/Wives_of_Henry_VIII', 'husband': 'king henry of england', 'wife': 'anne boleyn'}]}
Desired output:所需的 output:
name identifier WED spouse died yrs mth days husband wife
0 Henry VIII henry 11JUNE1509 TBC 0.00 23.0 11.0 12.0 king henry of england catherine of aragon
1 Henry VIII henry 28MAY1533 TBC 1.00 2.0 11.0 19.0 king henry of england anne boleyn
However, to date I have been only able to produce the following:但是,到目前为止,我只能制作以下内容:
name identifier WED spouse died yrs mth days husband wife
0 NaN NaN 11JUNE1509 TBC 0.00 NaN NaN NaN king henry of england catherine of aragon
1 NaN NaN 28MAY1533 TBC 1.00 NaN NaN NaN king henry of england anne boleyn
Below is the code I have created thus far:以下是我迄今为止创建的代码:
df = pd.DataFrame(test_dict['wives'], columns = ['name', 'identifier', 'WED','spouse', 'died', 'yrs', 'mth', 'days', 'husband', 'wife'])
print(df)
However, based on my output, I am having problems accessing name
, identifier
, yrs
, mth
, days
and can't figure it out.但是,根据我的 output,我在访问
name
、 identifier
、 yrs
、 mth
、 days
时遇到问题,无法弄清楚。
Try:尝试:
df = pd.DataFrame(test_dict["wives"])
df = pd.concat(
[
df,
df.pop("marriage").apply(pd.Series),
df.pop("marriage_lasted").apply(pd.Series),
],
axis=1,
)
cols = "name identifier WED spouse died yrs mth days husband wife".split()
print(df[cols].to_markdown(index=False))
Prints:印刷:
name![]() |
identifier![]() |
WED![]() |
spouse![]() |
died![]() |
yrs![]() |
mth![]() |
days![]() |
husband![]() |
wife![]() |
---|---|---|---|---|---|---|---|---|---|
Henry VIII![]() |
henry![]() |
11JUNE1509 ![]() |
TBC![]() |
0 ![]() |
23 ![]() |
11 ![]() |
12 ![]() |
king henry of england![]() |
catherine of aragon![]() |
Henry VIII![]() |
henry![]() |
28MAY1533 ![]() |
TBC![]() |
1 ![]() |
2 ![]() |
11 ![]() |
19 ![]() |
king henry of england![]() |
anne boleyn![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.