Pandas- dataframe ကို ကော်လံတန်ဖိုးဖြင့် မည်သို့ခွဲမည်နည်း။
ကော်လံတန်ဖိုးဖြင့် pandas DataFrame ကိုခွဲရန် အောက်ပါအခြေခံအထားအသိုကိုသုံးနိုင်သည်။
#define value to split on x = 20 #define df1 as DataFrame where 'column_name' is >= 20 df1 = df[df[' column_name '] >= x] #define df2 as DataFrame where 'column_name' is < 20 df2 = df[df[' column_name '] < x]
အောက်ဖော်ပြပါ ဥပမာသည် ဤ syntax ကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ပြသထားသည်။
ဥပမာ- Pandas DataFrame ကို Column Value ဖြင့် ခွဲပါ။
ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ' points ': [22, 24, 19, 18, 14, 29, 31, 16], ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print (df) team points rebounds 0 to 22 11 1 B 24 8 2 C 19 10 3 D 18 6 4 E 14 6 5 F 29 5 6 G 31 9 7:16:12
DataFrame ကို ပထမတစ်ခုတွင် “ မှတ်များ” ထက် 20 နှင့် ညီမျှသည့် အတန်းများပါရှိသော အတန်းများပါရှိသော DataFrame နှစ်ခုသို့ ခွဲရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပြီး ဒုတိယတွင် “ မှတ်များ” ထက်နည်းသော အတန်းများပါရှိသည်-
#define value to split on x = 20 #define df1 as DataFrame where 'points' is >= 20 df1 = df[df[' points '] >= x] print (df1) team points rebounds 0 to 22 11 1 B 24 8 5 F 29 5 6 G 31 9 #define df2 as DataFrame where 'points' is < 20 df2 = df[df[' points '] < x] print (df2) team points rebounds 2 C 19 10 3 D 18 6 4 E 14 6 7:16:12
ရလဒ် DataFrame တစ်ခုစီအတွက် အညွှန်းတန်ဖိုးများကို ပြန်လည်သတ်မှတ်ရန် reset_index() လုပ်ဆောင်ချက်ကိုလည်း အသုံးပြုနိုင်ကြောင်း သတိပြုပါ။
#define value to split on x = 20 #define df1 as DataFrame where 'points' is >= 20 df1 = df[df[' points '] >= x]. reset_index (drop= True ) print (df1) team points rebounds 0 to 22 11 1 B 24 8 2 F 29 5 3 G 31 9 #define df2 as DataFrame where 'points' is < 20 df2 = df[df[' points '] < x]. reset_index (drop= True ) print (df2) team points rebounds 0 C 19 10 1 D 18 6 2 E 14 6 3:16:12
ရလဒ် DataFrame တစ်ခုစီ၏ အညွှန်းသည် ယခု 0 တွင် စတင်သည်ကို သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြုပြင်ရမည်ကို ရှင်းပြသည်-
Pandas တွင် KeyError ကိုဘယ်လိုပြင်မလဲ။
ပြင်ဆင်နည်း- ValueError- float NaN ကို int သို့ ပြောင်းလဲ၍မရပါ။
ပြုပြင်နည်း- တန်ဖိုးအမှား- Operands များကို ပုံသဏ္ဍာန်များဖြင့် ထုတ်လွှင့်၍မရပါ။