Pandas dataframe ရှိ အညွှန်းအလိုက် ကော်လံများကို မည်သို့ရွေးချယ်ရမည်နည်း။
မကြာခဏဆိုသလို သင်သည် ၎င်းတို့၏ အညွှန်းတန်ဖိုးအပေါ် အခြေခံ၍ ပန်ဒါ DataFrame ရှိ ကော်လံများကို ရွေးချယ်လိုပေမည်။
ကိန်းပြည့်အညွှန်းကို အခြေခံ၍ ကော်လံများကို ရွေးချယ်လိုပါက၊ .iloc လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်သည်။
အညွှန်းညွှန်းကိန်းကို အခြေခံ၍ ကော်လံများကို ရွေးချယ်လိုပါက၊ .loc လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်ပါသည်။
ဤသင်ခန်းစာသည် ဤလုပ်ဆောင်ချက်တစ်ခုစီကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ဥပမာပေးထားသည်။
ဥပမာ 1- Integer Indexing ကို အခြေခံ၍ ကော်လံများကို ရွေးပါ။
အောက်ပါကုဒ်သည် pandas DataFrame ဖန်တီးနည်းကိုပြသပြီး .iloc ကို အသုံးပြု၍ ကိန်းပြည့်အညွှန်းတန်ဖိုး 3 ရှိသော ကော်လံကိုရွေးချယ်ရန်။
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 #select column with index position 3 df. iloc [:, 3] 0 11 1 8 2 10 3 6 4 6 5 5 Name: rebounds, dtype: int64
ကော်လံများစွာကို ရွေးချယ်ရန် အလားတူ syntax ကို အသုံးပြုနိုင်ပါသည်။
#select columns with index positions 1 and 3
df. iloc [:, [1, 3]]
rebound points
0 11 11
1 7 8
2 8 10
3 10 6
4 13 6
5 13 5
သို့မဟုတ် ကျွန်ုပ်တို့သည် အပိုင်းအခြားတစ်ခုအတွင်း ကော်လံအားလုံးကို ရွေးနိုင်သည်-
#select columns with index positions in range 0 through 3
df. iloc [:, 0:3]
team points assists
0 to 11 5
1 To 7 7
2 to 8 7
3 B 10 9
4 B 13 12
5 B 13 9
ဥပမာ 2- Label Indexing ကို အခြေခံ၍ ကော်လံများကို ရွေးပါ။
အောက်ပါကုဒ်သည် pandas DataFrame ဖန်တီးနည်းကိုပြသပြီး “ bounces” ၏ အညွှန်းတံဆိပ်ပါသော ကော်လံကိုရွေးချယ်ရန် .loc ကိုအသုံးပြုသည်-
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 #select column with index label 'rebounds' df. loc [:, ' rebounds '] 0 11 1 8 2 10 3 6 4 6 5 5 Name: rebounds, dtype: int64
မတူညီသော အညွှန်းအညွှန်းများပါရှိသော ကော်လံအများအပြားကို ရွေးချယ်ရန် ဆင်တူအထားအသိုကို အသုံးပြုနိုင်ပါသည်။
#select the columns with index labels 'points' and 'rebounds'
df. loc [:,[' points ',' rebounds ']]
rebound points
0 11 11
1 7 8
2 8 10
3 10 6
4 13 6
5 13 5
သို့မဟုတ် ကျွန်ုပ်တို့သည် အပိုင်းအခြားတစ်ခုအတွင်း ကော်လံအားလုံးကို ရွေးနိုင်သည်-
#select columns with index labels between 'team' and 'assists'
df. loc [:, ' team ':' assists ']
team points assists
0 to 11 5
1 To 7 7
2 to 8 7
3 B 10 9
4 B 13 12
5 B 13 9
ဆက်စပ်- Pandas loc vs iloc- ကွာခြားချက်ကား အဘယ်နည်း။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas DataFrame တွင် အညွှန်းအလိုက် အုပ်စုဖွဲ့နည်း
Pandas DataFrame တွင် အညွှန်းအလိုက် အတန်းများကို ရွေးချယ်နည်း
Pandas DataFrame တွင် အတန်းနံပါတ်များ ရယူနည်း
Pandas DataFrame ရှိ အညွှန်းကော်လံကို မည်ကဲ့သို့ ဖယ်ရှားနည်း