2
私はデータフレームに変換したいjson objectを持っています。jsonオブジェクトをPythonのデータフレームに変換する
:生データがどのように見えるか、私は 、列のデータフレームで列にそれを変換するために行う必要がありますid relid year activeyear ....
178312 ISR-1992-1-377-561 1992 TRUE ....
210197 CAO-2015-3-1076-210 1996 TRUE ....
0 {'id': 178312, 'relid': 'ISR-1992-1-377-561', ...
1 {'id': 210197, 'relid': 'CAO-2015-3-1076-210',...
2 {'id': 210203, 'relid': 'IRQ-2015-1-448-594', ...
3 {'id': 4233, 'relid': 'ALG-1995-3-1390-1', 'ye...
4 {'id': 76775, 'relid': 'SRI-1996-1-243-98', 'y...
:しかし、
df = pd.read_json("http://ucdpapi.pcr.uu.se/api/gedevents/5.0?pagesize=100&Geography=47%202,49%203")["Result"]
df
で、私はこれを取得します
{
"TotalCount": 5,
"TotalPages": 1,
"PreviousPageUrl": "",
"NextPageUrl": "",
"Result": [
{
"id": 178312,
"relid": "ISR-1992-1-377-561",
"year": 1992,
"active_year": true,
"code_status": "Clear",
"type_of_violence": 1,
"conflict_dset_id": "1-37",
"conflict_new_id": 234,
"conflict_name": "Israel:Palestine",
"dyad_dset_id": "377",
"dyad_new_id": 476,
"dyad_name": "Government of Israel - Fatah",
"side_a_dset_id": "666",
"side_a_new_id": 121,
"side_a": "Government of Israel",
"side_b_dset_id": "1049",
"side_b_new_id": 207,
"side_b": "Fatah",
........
ありがとうございました!
更新日: urllibの使用とインポート要求の処理。 enter image description here
:作品別の方法はjson = df.to_json(orient='index')
でPythonのオブジェクトに
0 {'id': 178312, 'relid': 'ISR-1992-1-377-561', ...
1 {'id': 210197, 'relid': 'CAO-2015-3-1076-210',...
2 {'id': 210203, 'relid': 'IRQ-2015-1-448-594', ...
3 {'id': 4233, 'relid': 'ALG-1995-3-1390-1', 'ye...
4 {'id': 76775, 'relid': 'SRI-1996-1-243-98', 'y...
を書き込み、
pd.read_json(json, orient="index")
最終的な出力が見えるJSONオブジェクトとしてそれを読み出すことです
ありがとうございました!ちょうどそれを試して、urllibが働いた。ジュピターノートはurllib2を認識していないようだ。 – user7999601