Pandas ရှိ အခြေအနေတစ်ခုအပေါ် အခြေခံ၍ ကော်လံအသစ်တစ်ခု ဖန်တီးနည်း


မကြာခဏဆိုသလို သင်သည် အချို့သော အခြေအနေများအပေါ် အခြေခံ၍ ပန်ဒါ DataFrame တွင် ကော်လံအသစ်တစ်ခုကို ဖန်တီးလိုပေမည်။

ဤသင်ခန်းစာတွင် အောက်ပါ DataFrame ကိုအသုံးပြု၍ ၎င်းကိုလုပ်ဆောင်ပုံ၏ ဥပမာများစွာကို ပေးသည် ။

 import pandas as pd
import numpy as np

#createDataFrame
df = pd. DataFrame ({'rating': [90, 85, 82, 88, 94, 90, 76, 75, 87, 86],
                   'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 8, 5, 7, 6, 9, 9, 5],
                   'rebounds': [11, 8, 10, 6, 6, 9, 6, 10, 10, 7]})

#view DataFrame
df

rating points assists rebounds
0 90 25 5 11
1 85 20 7 8
2 82 14 7 10
3 88 16 8 6
4 94 27 5 6
5 90 20 7 9
6 76 12 6 6
7 75 15 9 10
8 87 14 9 10
9 86 19 5 7

ဥပမာ 1- binary တန်ဖိုးများဖြင့် ကော်လံအသစ်တစ်ခုကို ဖန်တီးပါ။

ပေးထားသော အတန်းရှိ အမှတ်များသည် 20 ထက် များနေပြီး အခြားမဟုတ်ပါက “ မရှိ” ထက် တန်ဖိုး “ yes” ဖြစ်သည့် “ Good” ဟုခေါ်သော ကော်လံအသစ်ကို အောက်ပါကုဒ်က ပြသသည်-

 #create new column titled 'Good'
df['Good'] = np. where (df['points']>20, ' yes ', ' no ')

#view DataFrame 
df

        rating points assists rebounds Good
0 90 25 5 11 yes
1 85 20 7 8 no
2 82 14 7 10 no
3 88 16 8 6 no
4 94 27 5 6 yes
5 90 20 7 9 no
6 76 12 6 6 no
7 75 15 9 10 no
8 87 14 9 10 no
9 86 19 5 7 no

ဥပမာ 2- တန်ဖိုးများစွာဖြင့် ကော်လံအသစ်တစ်ခုကို ဖန်တီးပါ။

အောက်ပါကုဒ်သည် တန်ဖိုးရှိသည့်နေရာတွင် “ Good” ဟုခေါ်သော ကော်လံအသစ်ကို ဖန်တီးနည်းကို ပြသသည်-

  • အမှတ် ≥ 25 ဖြစ်ပါက “ဟုတ်ကဲ့”
  • 15 ≤ အမှတ် < 25 ဖြစ်လျှင် “ဖြစ်နိုင်သည်”
  • အမှတ် <15 ရှိလျှင် “No”
 #define function for classifying players based on points
def f(row):
    if row['points'] < 15:
        val = 'no'
    elif row['points'] < 25:
        val = 'maybe'
    else :
        val = 'yes'
    return val

#create new column 'Good' using the function above
df['Good'] = df. apply (f, axis=1)

#view DataFrame 
df

        rating points assists rebounds Good
0 90 25 5 11 yes
1 85 20 7 8 maybe
2 82 14 7 10 no
3 88 16 8 6 maybe
4 94 27 5 6 yes
5 90 20 7 9 maybe
6 76 12 6 6 no
7 75 15 9 10 maybe
8 87 14 9 10 no
9 86 19 5 7 maybe

ဥပမာ 3- ရှိပြီးသားကော်လံတစ်ခုနှင့် နှိုင်းယှဉ်မှုအပေါ် အခြေခံ၍ ကော်လံအသစ်တစ်ခု ဖန်တီးပါ။

အောက်ပါကုဒ်သည် တန်ဖိုးရှိသည့် “ assist_more” ဟုခေါ်သော ကော်လံအသစ်ကို မည်သို့ဖန်တီးရမည်ကို ပြသသည်-

  • ကူညီပေးလျှင် “ Yes” > ပြန်တက်လာသည်။
  • ‘မဟုတ်ဘူး’ မဟုတ်လား။
 #create new column titled 'assist_more'
df['assist_more'] = np. where (df['assists']>df['rebounds'], ' yes ', ' no ')

#view DataFrame 
df

        rating points assists rebounds assist_more
0 90 25 5 11 no
1 85 20 7 8 no
2 82 14 7 10 no
3 88 16 8 6 yes
4 94 27 5 6 no
5 90 20 7 9 no
6 76 12 6 6 no
7 75 15 9 10 no
8 87 14 9 10 no
9 86 19 5 7 no

နောက်ထပ် Python သင်ခန်းစာများကို ဤနေရာတွင် ရှာနိုင်သည်။

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

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