Pandas- စာရင်းထဲမပါသောကော်လံများကို ဖယ်ရှားနည်း
တိကျသောစာရင်းတွင်မရှိသော pandas DataFrame မှကော်လံများကိုဖယ်ရှားရန် အောက်ပါအခြေခံ syntax ကိုသင်အသုံးပြုနိုင်သည်-
#define columns to keep keep_cols = [' col1 ', ' col2 ', ' col3 '] #create new dataframe by dropping columns not in list new_df = df[df. columns . intersection (keep_cols)]
ဤဥပမာသည် col1 ၊ col2 သို့မဟုတ် col3 နှင့်မညီသော DataFrame မှကော်လံများကိုဖယ်ရှားပါမည်။
အောက်ဖော်ပြပါ ဥပမာသည် ဤ syntax ကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ပြသထားသည်။
ဥပမာ- Pandas တွင် စာရင်းမဝင်သော ကော်လံများကို ဖယ်ရှားပါ။
အမျိုးမျိုးသော ဘတ်စကတ်ဘောကစားသမားများအကြောင်း အချက်အလက်ပါရှိသော အောက်ပါပန်ဒါ DataFrame ရှိသည်ဆိုပါစို့။
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ' points ': [18, 22, 19, 14, 14, 11, 20, 28], ' assists ': [5, 7, 7, 9, 12, 9, 9, 4], ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12], ' steals ': [4, 4, 10, 12, 8, 5, 5, 2]}) #view DataFrame print (df) team points assists rebounds steals 0 A 18 5 11 4 1 B 22 7 8 4 2 C 19 7 10 10 3 D 14 9 6 12 4 E 14 12 6 8 5 F 11 9 5 5 6 G 20 9 9 5 7:28 4 12 2
ယခု ကျွန်ုပ်တို့သည် အောက်ပါကော်လံစာရင်းတွင်မရှိသော ကော်လံအားလုံးကို ဖယ်ရှားသည့် DataFrame အသစ်တစ်ခုကို ဖန်တီးလိုသည်ဆိုပါစို့- team ၊ points နှင့် Steles .
ဒါကိုလုပ်ဖို့ အောက်ပါ syntax ကို သုံးနိုင်ပါတယ်။
#define columns to keep keep_cols = [' team ', ' points ', ' steals '] #create new dataframe by dropping columns not in list new_df = df[df. columns . intersection (keep_cols)] #view new dataframe print (new_df) team points steals 0 to 18 4 1 B 22 4 2 C 19 10 3 D 14 12 4 E 14 8 5 F 11 5 6 G 20 5 7:28 a.m. 2
Keep_cols စာရင်းတွင်မပါရှိသော မူရင်း DataFrame ရှိ ကော်လံတစ်ခုစီကို DataFrame အသစ်မှ ဖယ်ရှားလိုက်ကြောင်း သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် အခြားဘုံပန်ဒါတာဝန်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
ပန်ဒါရှိ ပထမတန်းကို ဖယ်ရှားနည်း
Pandas ရှိ ပထမကော်လံကို ဘယ်လိုဖယ်ရှားမလဲ။
Pandas ရှိ ထပ်နေသောကော်လံများကို ဖယ်ရှားနည်း
Pandas အချို့မှလွဲ၍ ကော်လံအားလုံးကို ဖျက်နည်း