Pandas တွင် အချို့သောကော်လံများကို မည်သို့ထိန်းသိမ်းနည်း (ဥပမာများဖြင့်)


Pandas DataFrame တွင် အချို့သောကော်လံများကိုသာ ဆက်လက်ထားရှိရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-

နည်းလမ်း 1- မည်သည့်ကော်လံများကို သိမ်းဆည်းရမည်ကို သတ်မှတ်ပါ။

 #only keep columns 'col1' and 'col2'
df[[' col1 ', ' col2 ']]

နည်းလမ်း 2- ဖျက်ရန် ကော်လံများကို သတ်မှတ်ပါ။

 #drop columns 'col3' and 'col4'
df[df. columns [~df. columns . isin ([' col3 ',' col4 '])]]

အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'B', 'B', 'B'],
                   ' points ': [11, 7, 8, 10, 13, 13],
                   ' assists ': [5, 7, 7, 9, 12, 9],
                   ' rebounds ': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

	team points assists rebounds
0 A 11 5 11
1 To 7 7 8
2 to 8 7 10
3 B 10 9 6
4 B 13 12 6
5 B 13 9 5

နည်းလမ်း 1- မည်သည့်ကော်လံများကို သိမ်းဆည်းရမည်ကို သတ်မှတ်ပါ။

အောက်ပါကုဒ်သည် “အဖွဲ့” နှင့် “အမှတ်များ” ကော်လံများကိုသာ ထိန်းသိမ်းထားသည့် DataFrame အသစ်ကို မည်သို့သတ်မှတ်ရမည်ကို ပြသသည်-

 #create new DataFrame and only keep 'team' and 'points' columns
df2 = df[[' team ', ' points ']]

#view new DataFrame
df2

        team points
0 to 11
1 to 7
2 to 8
3 B 10
4 B 13
5 B 13

ရလဒ် DataFrame သည် ကျွန်ုပ်တို့သတ်မှတ်ထားသော ကော်လံနှစ်ခုကိုသာ သိမ်းဆည်းထားကြောင်း သတိပြုပါ။

နည်းလမ်း 2- ဖျက်ရန် ကော်လံများကို သတ်မှတ်ပါ။

အောက်ဖော်ပြပါကုဒ်သည် မူရင်း DataFrame မှ “ တက်ရောက်သည်” နှင့် “ ဘောင်ဝင်သည်” ကော်လံများကို ဖယ်ရှားသည့် DataFrame အသစ်ကို သတ်မှတ်နည်းကို ပြသသည်-

 #create new DataFrame and that drops 'assists' and 'rebounds'
df2 = df[df. columns [~df. columns . isin ([' assists ', ' rebounds '])]]

#view new DataFrame
df2

        team points
0 to 11
1 to 7
2 to 8
3 B 10
4 B 13
5 B 13

ရလဒ် DataFrame သည် မူရင်း DataFrame မှ “ assists” နှင့် “ bounces” ကော်လံများကို ဖယ်ရှားပြီး ကျန်ကော်လံများကို ထိန်းသိမ်းထားကြောင်း သတိပြုပါ။

ထပ်လောင်းအရင်းအမြစ်များ

အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-

Pandas DataFrame ရှိ ပထမကော်လံကို ဘယ်လိုဖယ်ရှားမလဲ။
Pandas ရှိ ထပ်နေသောကော်လံများကို ဖယ်ရှားနည်း
Pandas ရှိ အညွှန်းအားဖြင့် ကော်လံများကို ဖျက်နည်း

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်