我正在研究json規范化的方法。它給了我這個解決方案。
json = {"vehicle_type":"Car", "car_info": {"count":3,"infos":[{"car": "BMW","name":"5", "description": "","production_time":"2017-07-16","標簽": ["car","BMW","sedan"],"references":[],"nountry": ["germany"],"fuel_type":["electrical"],"color": "black","price":null,"technic":{"0-100": "-","馬力":"165Nm"}, "里程": {"mile":14004}}]}}。
df = json_normalize(json)
當我試圖決議我的嵌套json時,在一些列中仍然有更多的字典。 像這樣的代碼
。df_car = pd.DataFrame()
empty_list = pd.DataFrame()
empty_list = [{}]
for i in range(len(df1)):
if df1["car_info.infos"][i] == [{}]。
df_car = df_car.append(empty_list,ignore_index = True)
else:
car_info = (pd.DataFrame.from_dict(df1["car_info.infos"][i][0], orient='index'>)
car_info=car_info.transpose()
df_car = df_car.append(car_info,ignore_index=True)
df2 = pd.concat([df1,df_car], axis = 1)
df2 = df2.drop(columns={"car_info.infos"})
我需要沒有列名的代碼來決議嵌套的json,直到所有的dicts都不存在。我有多個json檔案。如何才能使其自動化?
uj5u.com熱心網友回復:
你可以使用pd.json_normalize(),引數為record_path=和meta=,如下:
df = pd. json_normalize(json, record_path=['car_info'/span>, 'infos'/span>]。meta=['vehicle_type', ['car_info', 'count']])
結果:
print(df)
汽車名稱 描述 生產_時間 標簽 參考資料 國家 燃料型別 顏色 價格 技術.0-100 技術.馬力 mileage.mile 車輛型別 car_info.count
0 BMW 5 2017-07-16 [ car, 寶馬, 轎車] [] [德國] [電氣] 黑色 NaN - 165Nm 14004 Car 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/315098.html
標籤:
上一篇:SQLServer:使用CTE行磁區來序列化連續的時間戳
下一篇:在EVM區塊鏈中存盤秘密
