Pandas- ကိန်းဂဏန်းများအဖြစ် strings များကို စာဝှက်ရန် factorize() ကိုအသုံးပြုနည်း
pandas factorize() လုပ်ဆောင်ချက်ကို ဂဏန်းတန်ဖိုးများအဖြစ် စာကြောင်းများကို ကုဒ်လုပ်ရန် အသုံးပြုနိုင်သည်။
ပန်ဒါ DataFrame ကော်လံများတွင် factorize() လုပ်ဆောင်ချက်ကို အသုံးပြုရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်ပါသည်။
နည်းလမ်း 1- ကော်လံတစ်ခုကို အချက်ပြပါ။
df[' col1 '] = pd. factorize (df[' col '])[0]
နည်းလမ်း 2- သီးခြားကော်လံများကို အချက်ပြပါ။
df[[' col1 ', ' col3 ']] = df[[' col1 ', ' col3 ']]. apply ( lambda x: pd.factorize (x)[ 0 ])
နည်းလမ်း 3- ကော်လံအားလုံးကို အချက်ပြပါ။
df = df. apply ( lambda x: pd.factorize (x)[ 0 ])
အောက်ဖော်ပြပါ ဥပမာသည် အောက်ပါ pandas DataFrame ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-
import pandas as pd #createDataFrame df = pd. DataFrame ({' conf ': ['West', 'West', 'East', 'East'], ' team ': ['A', 'B', 'C', 'D'], ' position ': ['Guard', 'Forward', 'Guard', 'Center'] }) #view DataFrame df conf team position 0 West A Guard 1 West B Forward 2 East C Guard 3 East D Center
ဥပမာ 1- ကော်လံတစ်ခုကို အချက်ပြပါ။
အောက်ပါကုဒ်သည် DataFrame တွင် ကော်လံတစ်ခုအား ကိန်းဂဏာန်းပုံပြသည်-
#factorize the conf column only df[' conf '] = pd. factorize (df[' conf '])[ 0 ] #view updated DataFrame df conf team position 0 0 A Guard 1 0 B Forward 2 1 C Guard 3 1 D Center
‘conf’ ကော်လံကိုသာ ပိုင်းခြားထားသည်ကို သတိပြုပါ။
“ အနောက်” ဖြစ်ခဲ့သော တန်ဖိုးတိုင်းသည် ယခုအခါ 0 ဖြစ်ပြီး “ အရှေ့” သည် ယခုအခါ 1 ဖြစ်သည်။
ဥပမာ 2- သီးခြားကော်လံများကို အချက်ပြပါ။
အောက်ဖော်ပြပါ ကုဒ်သည် DataFrame တွင် သီးခြားကော်လံများကို ခွဲခြမ်းနည်းကို ပြသသည်-
#factorize conf and team columns only df[[' conf ', ' team ']] = df[[' conf ', ' team ']]. apply ( lambda x: pd.factorize (x)[ 0 ]) #view updated DataFrame df conf team position 0 0 0 Guard 1 0 1 Forward 2 1 2 Guard 3 1 3 Center
“ conf” နှင့် “ team” ကော်လံနှစ်ခုလုံးကို ပိုင်းဖြတ်ထားကြောင်း သတိပြုပါ။
ဥပမာ 3- ကော်လံအားလုံးကို အချက်ပြပါ။
အောက်ပါ ကုဒ်သည် DataFrame တွင် ကော်လံများအားလုံးကို ကိန်းဂဏာန်းပုံပြသည်-
#factorize all columns df = df. apply ( lambda x: pd.factorize (x)[ 0 ]) #view updated DataFrame df conf team position 0 0 0 0 1 0 1 1 2 1 2 0 3 1 3 2
ကော်လံအားလုံးကို ပိုင်းဖြတ်ပြီးကြောင်း သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံလုပ်ဆောင်ချက်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas DataFrame ကော်လံများကို စာကြောင်းများအဖြစ်သို့ ပြောင်းလဲနည်း
Pandas တွင် categorical variable ကို ဂဏန်းအဖြစ် မည်သို့ပြောင်းရမည်နည်း။
Pandas DataFrame ကော်လံများကို ကိန်းပြည့်အဖြစ် မည်သို့ပြောင်းလဲမည်နည်း။