Pandas တွင် ကော်လံတစ်ခုရှိမရှိ စစ်ဆေးနည်း (ဥပမာများဖြင့်)
Pandas DataFrame တွင် ကော်လံတစ်ခုရှိမရှိ စစ်ဆေးရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်ပါသည်။
နည်းလမ်း 1- ကော်လံရှိမရှိ စစ်ဆေးပါ။
' column1 ' in df. columns
DataFrame တွင် “ column1” ရှိနေပါက ၎င်းသည် True ပြန်သွားလိမ့်မည်၊ မဟုတ်ပါက False ပြန်သွားပါမည်။
နည်းလမ်း 2- ကော်လံအများအပြားရှိမရှိ စစ်ဆေးပါ။
{' column1 ', ' column2 '}. issubset ( df.columns )
DataFrame တွင် “ column1” နှင့် “ column2” ရှိနေပါက ၎င်းသည် True ပြန်သွားလိမ့်မည်၊ မဟုတ်ပါက False ပြန်လာပါမည်။
အောက်ဖော်ပြပါနမူနာများသည် အောက်ပါ 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]}) #view DataFrame print (df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7:28 4 12
ဥပမာ 1- ကော်လံရှိမရှိ စစ်ဆေးပါ။
DataFrame တွင် ‘အဖွဲ့’ ကော်လံရှိမရှိ ကြည့်ရှုရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပါသည်။
#check if 'team' column exists in DataFrame
' team ' in df. columns
True
“ အဖွဲ့” ကော်လံသည် DataFrame တွင် ရှိနေသောကြောင့် ပန်ဒါများသည် True value ကို ပြန်ပေးသည်။
“ team” ကော်လံရှိလျှင် လုပ်ဆောင်ချက်တစ်ခုကို လုပ်ဆောင်ရန် if statement ကိုလည်း အသုံးပြုနိုင်ပါသည်။
#if 'team' exists, create new column called 'team_name'
if ' team ' in df. columns :
df[' team_name '] = df[' team ']
#view updated DataFrame
print (df)
team points assists rebounds team_name
0 A 18 5 11 A
1 B 22 7 8 B
2 C 19 7 10 C
3 D 14 9 6 D
4 E 14 12 6 E
5 F 11 9 5 F
6 G 20 9 9 G
7:28 a.m. 4:12 p.m.
ဥပမာ 2- ကော်လံအများအပြားရှိမရှိ စစ်ဆေးပါ။
DataFrame တွင် ‘team’ နှင့် ‘player’ ကော်လံများ ရှိနေခြင်းရှိမရှိ ကြည့်ရှုရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပါသည်။
#check if 'team' and 'player' columns both exist in DataFrame
{' team ', ' player '}. issubset ( df.columns )
False
‘အဖွဲ့’ ကော်လံသည် DataFrame တွင် ရှိနေသော်လည်း ‘ကစားသမား’ မပါသောကြောင့် ပန်ဒါများသည် မှားယွင်းသော တန်ဖိုးကို ပြန်ပေးသည်။
DataFrame တွင် “ points” နှင့် “ assists” များရှိမရှိကို ကြည့်ရှုရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်သည်။
#check if 'points' and 'assists' columns both exist in DataFrame
{' points ', ' assists '}. issubset ( df.columns )
True
ကော်လံနှစ်ခုလုံးရှိသောကြောင့် ပန်ဒါများသည် True value ကို ပြန်ပေးသည်။
ထို့နောက် “ points” နှင့် “ helpers” များရှိလျှင် လုပ်ဆောင်ချက်တစ်ခုလုပ်ဆောင်ရန် if statement ကိုသုံးနိုင်သည်-
#if both exist, create new column called 'total' that finds sum of points and assists
if {' points ', ' assists '}. issubset ( df.columns ):
df[' total '] = df[' points '] + df[' assists ']
#view updated DataFrame
print (df)
team points assists rebounds total
0 A 18 5 11 23
1 B 22 7 8 29
2 C 19 7 10 26
3 D 14 9 6 23
4 E 14 12 6 26
5 F 11 9 5 20
6 G 20 9 9 29
7:28 4 12 32
DataFrame တွင် “ points” နှင့် “ assists” နှစ်ခုလုံးရှိသောကြောင့်၊ ပန်ဒါများသည် “ total” ဟုခေါ်သော “ points” နှင့် “ assists” ကော်လံများပေါင်းကိုပြသသည့် “ total” ကော်လံအသစ်ကို ဖန်တီးခဲ့သည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas ရှိ အချို့သောကော်လံများကို မည်သို့ထိန်းသိမ်းနည်း
Pandas ရှိ အညွှန်းအလိုက် ကော်လံများကို မည်သို့ရွေးချယ်ရမည်နည်း။
Pandas တွင်ကော်လံတစ်ခုရွှေ့နည်း