假设json_object
的每个值都是dict
,也可以使用以下方法:
df = pd.DataFrame(
data = {
"customer_id": ["A101", "A102"],
"date": ["2022-06-21", "2022-06-22"],
"json_object": [{'name': 'james','age':55, 'hobby':'pubg'}, {'name': 'tarzan','status':'single'}]
}
)
df["json_object"] = df["json_object"].map(lambda x: [[i, x[i]] for i in x])
df = df.explode(column="json_object")
df.json_object = df.json_object.str[0].astype(str) + ": " + df.json_object.str[1].astype(str)
df
------------------------------------------
customer_id date json_object
0 A101 2022-06-21 name: james
0 A101 2022-06-21 age: 55
0 A101 2022-06-21 hobby: pubg
1 A102 2022-06-22 name: tarzan
1 A102 2022-06-22 status: single
------------------------------------------
编辑
因为您将数据框更改为
df = pd.DataFrame(
data = {
"customer_id": ["A101", "A102"],
"date": ["2022-06-21", "2022-06-22"],
"json_object": [{'name': ['james'],'age':[55], 'hobby':['pubg']}, {'name': ['tarzan'],'status':['single']}]
}
)
我的代码必须调整如下:
df = pd.DataFrame(
data = {
"customer_id": ["A101", "A102"],
"date": ["2022-06-21", "2022-06-22"],
"json_object": [{'name': ['james'],'age':[55], 'hobby':['pubg']}, {'name': ['tarzan'],'status':['single']}]
}
)
df["json_object"] = df["json_object"].map(lambda x: [[i, x[i][0]] for i in x])
df = df.explode(column="json_object")
df.json_object = df.json_object.str[0].astype(str) + ": " + df.json_object.str[1].astype(str)
df
如果包含空列表,只需在lambda
函数中添加if-else
条件即可.注意,我还在下一个代码提取中重命名了这些列.
df = pd.DataFrame(
data = {
"customer_id": ["A101", "A102"],
"date": ["2022-06-21", "2022-06-22"],
"json_object": [{'name': ['james'],'age':[55], 'hobby':['pubg']}, {'name': ['tarzan'],'status':[]}]
}
)
df["json_object"] = df["json_object"].map(lambda x: [[i, x[i][0]] if x[i] else [i, ""] for i in x])
df = df.rename(columns={"json_object": "attribute"}).explode(column="attribute")
df.attribute = df.attribute.str[0].astype(str) + ": " + df.attribute.str[1].astype(str)