Pandas dataframe ကို json သို့ ဘယ်လိုပြောင်းမလဲ။
မကြာခဏဆိုသလို သင်သည် pandas DataFrame ကို JSON ဖော်မတ်သို့ ပြောင်းရန် စိတ်ဝင်စားနေပေမည်။
ကံကောင်းထောက်မစွာဖြင့် ၊ အောက်ပါဖော်မတ်များထဲမှတစ်ခုဖြင့် DataFrame ကို JSON string သို့ပြောင်းရန်ခွင့်ပြုသော to_json() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ၎င်းသည် လွယ်ကူစွာလုပ်ဆောင်နိုင်သည် –
- ‘split’- dict ကဲ့သို့ {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
- ‘မှတ်တမ်းများ’- စာရင်းကဲ့သို့သော [{ကော်လံ -> တန်ဖိုး}၊ …၊ {ကော်လံ -> တန်ဖိုး}]
- ‘index’- dict ကဲ့သို့ {index -> {column -> value}}
- ‘ကော်လံ’- {ကော်လံ -> {အညွှန်း -> တန်ဖိုး}} ကဲ့သို့ စကားလုံး
- ‘တန်ဖိုးများ’- တန်ဖိုးများ array မျှသာ
- ‘ဇယား’- {‘schema’၊ {schema}၊ ‘ဒေတာ’- {data}} ကဲ့သို့သော စကားလုံး
ဤသင်ခန်းစာတွင် အောက်ပါ pandas DataFrame ကို အသုံးပြု၍ ဖော်မတ်ခြောက်ခုစီသို့ DataFrame ကို မည်သို့ပြောင်းရမည်ကို ပြသသည် ။
import pandas as pd #createDataFrame df = pd.DataFrame({'points': [25, 12, 15, 19], 'assists': [5, 7, 7, 12]}) #view DataFrame df assist points 0 25 5 1 12 7 2 15 7 3 19 12
နည်းလမ်း 1: “ Divide”
df. to_json (orient=' split ') { "columns": [ "points", "assists" ], "index": [ 0, 1, 2, 3 ], "data": [ [ 25, 5 ], [ 12, 7 ], [ 15, 7 ], [ 19, 12 ] ] }
နည်းလမ်း 2- “ မှတ်တမ်းများ”
df. to_json (orient=' records ') [ { "points": 25, “assists”: 5 }, { "points": 12, “assists”: 7 }, { "points": 15, “assists”: 7 }, { "points": 19, “assists”: 12 } ]
နည်းလမ်း 3- “ အညွှန်းကိန်း”
df. to_json (orient=' index ') { "0": { "points": 25, “assists”: 5 }, "1": { "points": 12, “assists”: 7 }, "2": { "points": 15, “assists”: 7 }, "3": { "points": 19, “assists”: 12 } }
နည်းလမ်း 4- “ ကော်လံများ”
df. to_json (orient=' columns ') { "dots": { "0": 25, "1": 12, "2": 15, "3": 19 }, "assists": { "0": 5, "1": 7, "2": 7, "3": 12 } }
နည်းလမ်း 5- “ တန်ဖိုးများ”
df. to_json (orient=' values ') [ [ 25, 5 ], [ 12, 7 ], [ 15, 7 ], [ 19, 12 ] ]
နည်းလမ်း 6- “ ဇယား”
df. to_json (orient=' table ') { "plan": { "fields": [ { "name": "index", "type": "integer" }, { "name": "points", "type": "integer" }, { "name": "assists", "type": "integer" } ], "primaryKey": [ "index" ], "pandas_version": "0.20.0" }, "data": [ { "index": 0, "points": 25, “assists”: 5 }, { "index": 1, "points": 12, “assists”: 7 }, { "index": 2, "points": 15, “assists”: 7 }, { "index": 3, "points": 19, “assists”: 12 } ] }
JSON ဖိုင်ကို ဘယ်လိုထုတ်မလဲ။
သင့်ကွန်ပြူတာရှိ သီးခြားဖိုင်လမ်းကြောင်းတစ်ခုသို့ JSON ဖိုင်ကို တင်ပို့ရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်သည်။
#create JSON file json_file = df. to_json (orient=' records ') #export JSON file with open('my_data.json', 'w') as f: f.write(json_file)
pandas to_json() လုပ်ဆောင်ချက်၏ စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာတွေ့နိုင်ပါသည် ။