[英]How to convert array of dictionary of array to pandas dataframe?
I have an array of nested dictionary:我有一个嵌套字典数组:
data = {"A":"a","B":"b","ID":[{"ii":"ABC","jj":"BCD"},{"ii":"AAC","jj":"FFD"}],"Finish":"yes"}数据 = {"A":"a","B":"b","ID":[{"ii":"ABC","jj":"BCD"},{"ii":"AAC" ,"jj":"FFD"}],"完成":"是"}
I used,我用了,
res = pd.DataFrame.from_dict(data , orient='index')
But the ID is still returned as list of dictionary.但是 ID 仍然作为字典列表返回。
A B ID Finish
a b [{"ii":"ABC","jj":"BCD"},{"aa":"AAC","bb":"FFD"}] yes
But I want everything to be converted to df.但我希望所有内容都转换为 df。 Not sure how to do it.
不知道该怎么做。 Kindly help.
请帮忙。
Expected Output:预期 Output:
A B ID.ii ID.jj Finish
a b ABC BCD yes
a b AAC FFD yes
You can achieve this using pandas json_normalize
您可以使用 pandas
json_normalize
实现此目的
df = pd.json_normalize(data, meta=['A', 'B'], record_path=['ID'], record_prefix="ID.")
Output Output
ID.ii ID.jj A B
0 ABC BCD a b
1 AAC FFD a b
record_path - will be used to flatten the specific key record_prefix - is added as a column prefix meta - is the columns that needs to be preserved without flattening record_path - 将用于展平特定键 record_prefix - 添加为列前缀 meta - 是需要保留而不展平的列
Refer the documentation for examples有关示例,请参阅文档
To achieve this without using json_normalize
, you can pre-process the input like this-要在不使用
json_normalize
的情况下实现这一点,您可以像这样预处理输入 -
data = {"A":"a","B":"b","ID":[{"ii":"ABC","jj":"BCD"},{"ii":"AAC","jj":"FFD"}],"Finish":"yes"}
op = {}
for i in data:
if isinstance(data[i], list):
for j in data[i]:
for k in j:
tmp = str(i)+"."+str(k)
if tmp not in op:
op[tmp] = [j[k]]
else:
op[tmp].append(j[k])
else:
op[i] = data[i]
>>> data
{'A': 'a', 'B': 'b', 'ID': [{'ii': 'ABC', 'jj': 'BCD'}, {'ii': 'AAC', 'jj': 'FFD'}], 'Finish': 'yes'}
>>> op
{'A': 'a', 'B': 'b', 'ID.ii': ['ABC', 'AAC'], 'ID.jj': ['BCD', 'FFD'], 'Finish': 'yes'}
After this you can directly use之后就可以直接使用
>>> pd.DataFrame(op)
A B ID.ii ID.jj Finish
0 a b ABC BCD yes
1 a b AAC FFD yes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.