Pandas- dataframe ကို အဘိဓာန်တစ်ခုသို့ အမြန်ပြောင်းပါ။
Pandas DataFrame ကို အဘိဓာန်တစ်ခုအဖြစ်သို့ ပြောင်းလဲရန် အောက်ပါ syntax ကို သင်သုံးနိုင်သည်။
df. to_dict ()
to_dict() သည် အောက်ပါ ဖြစ်နိုင်ချေရှိသော အကြောင်းပြချက်များကို လက်ခံကြောင်း သတိပြုပါ။
- dict- (မူလ) သော့များသည် ကော်လံအမည်များဖြစ်သည်။ တန်ဖိုးများသည် index:data pairs ၏ အဘိဓာန်များဖြစ်သည်။
- list- သော့များသည် ကော်လံအမည်များဖြစ်သည်။ တန်ဖိုးများသည် ကော်လံဒေတာစာရင်းများဖြစ်သည်။
- စီးရီး- ကီးများသည် ကော်လံအမည်များဖြစ်သည်။ တန်ဖိုးများသည် ကော်လံဒေတာစီးရီးများဖြစ်သည်။
- ခွဲထုတ်ခြင်း- သော့များသည် “ကော်လံများ”၊ “ဒေတာ” နှင့် “အညွှန်း” ဖြစ်သည်။
- မှတ်တမ်းများ- သော့များသည် ကော်လံအမည်များဖြစ်သည်။ တန်ဖိုးများသည် ဆဲလ်များရှိ ဒေတာများဖြစ်သည်။
- index- သော့များသည် အညွှန်းအညွှန်းများဖြစ်သည်။ တန်ဖိုးများသည် ဆဲလ်များရှိ ဒေတာများဖြစ်သည်။
အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ pandas DataFrame နှင့် လက်တွေ့တွင် ဤ syntax ကိုမည်သို့အသုံးပြုရမည်ကို ပြသသည် ။
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'A', 'B', 'B', 'C'], ' points ': [5, 7, 9, 12, 9], ' rebounds ': [11, 8, 6, 6, 5]}) #view DataFrame df team points rebounds 0 to 5 11 1 to 7 8 2 B 9 6 3 B 12 6 4 C 9 5
ဥပမာ 1- DataFrame ကို အဘိဓာန် (“ dict” ) သို့ ပြောင်းပါ
အောက်ပါကုဒ်သည် ပုံသေ ‘ dict ‘ နည်းလမ်းကို အသုံးပြု၍ pandas DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict ()
{'team': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'C'},
'points': {0:5, 1:7, 2:9, 3:12, 4:9},
'rebounds': {0:11, 1:8, 2:6, 3:6, 4:5}}
ဥပမာ 2- DataFrame ကို Dictionary (“ စာရင်း” ) သို့ ပြောင်းပါ
အောက်ပါကုဒ်သည် ‘ စာရင်း ‘ နည်းလမ်းကို အသုံးပြု၍ ပန်ဒါ DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict (' list ') {'team': ['A', 'A', 'B', 'B', 'C'], 'points': [5, 7, 9, 12, 9], 'rebounds': [11, 8, 6, 6, 5]}
ဥပမာ 3- DataFrame ကို အဘိဓာန် (“ စီးရီး” ) သို့ ပြောင်းပါ
အောက်ပါကုဒ်သည် ‘ စီးရီး ‘ နည်းလမ်းကို အသုံးပြု၍ ပန်ဒါ DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict (' series ') {'team': 0 A 1A 2 B 3 B 4C Name: team, dtype: object, 'points': 0 5 1 7 2 9 3 12 4 9 Name: points, dtype: int64, 'rebounds': 0 11 1 8 2 6 3 6 4 5 Name: rebounds, dtype: int64}
ဥပမာ 4- DataFrame ကို အဘိဓာန်တစ်ခုသို့ ပြောင်းပါ (“ ခွဲခြမ်း” )
အောက်ပါကုဒ်သည် ‘ ခွဲခြမ်း ‘ နည်းလမ်းကို အသုံးပြု၍ pandas DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict (' split ') {'index': [0, 1, 2, 3, 4], 'columns': ['team', 'points', 'rebounds'], 'data': [['A', 5, 11], ['A', 7, 8], ['B', 9, 6], ['B', 12, 6], ['C', 9, 5]]}
ဥပမာ 5- DataFrame ကို အဘိဓာန် (“ မှတ်တမ်းများ” ) သို့ ပြောင်းပါ
အောက်ပါကုဒ်သည် ‘ မှတ်တမ်း ‘ နည်းလမ်းကို အသုံးပြု၍ ပန်ဒါ DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict (' records ') [{'team': 'A', 'points': 5, 'rebounds': 11}, {'team': 'A', 'points': 7, 'rebounds': 8}, {'team': 'B', 'points': 9, 'rebounds': 6}, {'team': 'B', 'points': 12, 'rebounds': 6}, {'team': 'C', 'points': 9, 'rebounds': 5}]
ဥပမာ 6- DataFrame ကို အဘိဓာန် (“ အညွှန်း” ) သို့ ပြောင်းပါ
အောက်ပါကုဒ်သည် ‘ အညွှန်း ‘ နည်းလမ်းကို အသုံးပြု၍ ပန်ဒါ DataFrame ကို အဘိဓာန်သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
df. to_dict (' index ') {0: {'team': 'A', 'points': 5, 'rebounds': 11}, 1: {'team': 'A', 'points': 7, 'rebounds': 8}, 2: {'team': 'B', 'points': 9, 'rebounds': 6}, 3: {'team': 'B', 'points': 12, 'rebounds': 6}, 4: {'team': 'C', 'points': 9, 'rebounds': 5}}
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားသော ဘုံဒေတာကူးပြောင်းမှုများကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas DataFrame ကို NumPy array သို့ ဘယ်လိုပြောင်းမလဲ။
Pandas စီးရီးကို NumPy array သို့ ဘယ်လိုပြောင်းမလဲ။
Pandas DataFrame ကို List သို့ဘယ်လိုပြောင်းမလဲ။