Pandas တွင် categorical variable ကို ဂဏန်းအဖြစ် မည်သို့ပြောင်းရမည်နည်း။


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

 df[' column_name '] = pd. factorize (df[' column_name '])[0]

DataFrame ရှိ အမျိုးအစားအလိုက် ကွဲလွဲချက်တစ်ခုစီကို ကိန်းဂဏာန်းပြောင်းလဲနိုင်သောအဖြစ်သို့ ပြောင်းလဲရန် အောက်ပါ syntax ကို သင်အသုံးပြုနိုင်သည်။

 #identify all categorical variables
cat_columns = df. select_dtypes ([' object ']). columns

#convert all categorical variables to numeric
df[cat_columns] = df[cat_columns]. apply ( lambda x: pd.factorize (x)[ 0 ])

အောက်ပါဥပမာများသည် ဤ syntax ကိုလက်တွေ့တွင်မည်သို့အသုံးပြုရမည်ကိုပြသထားသည်။

ဥပမာ 1- Categorical Variable ကို ဂဏန်းအဖြစ် ပြောင်းပါ။

ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။

 import pandas as pd

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

#view DataFrame
df

team position points rebounds
0 A G 5 11
1 A G 7 8
2 A F 7 10
3 B G 9 6
4 B F 12 6
5 B C 9 5
6 C G 9 9
7 C F 4 12
8 C C 13 10

“ အဖွဲ့” ကော်လံကို ဂဏန်းအဖြစ် ပြောင်းလဲရန် အောက်ပါ syntax ကို သုံးနိုင်သည်-

 #convert 'team' column to numeric
df[' team '] = pd. factorize (df[' team '])[ 0 ]

#view updated DataFrame
df

team position points rebounds
0 0 G 5 11
1 0 G 7 8
2 0 F 7 10
3 1 G 9 6
4 1 F 12 6
5 1 C 9 5
6 2 G 9 9
7 2 F 4 12
8 2 C 13 10

ဤအရာသည် ပြောင်းလဲခြင်းသို့ရောက်ရှိသွားသည်-

  • A ” တန်ဖိုးရှိသော အသင်းတိုင်းကို 0 အဖြစ် ပြောင်းလဲခဲ့သည်။
  • B ” တန်ဖိုးရှိသော အသင်းတိုင်းကို 1 အဖြစ်သို့ ပြောင်းလဲခဲ့သည်။
  • C ” တန်ဖိုးရှိသော အသင်းတိုင်းကို 2 အဖြစ်သို့ ပြောင်းလဲခဲ့သည်။

ဥပမာ 2- များစွာသော အမျိုးအစားအလိုက် ကိန်းရှင်များကို ဂဏန်းတန်ဖိုးများအဖြစ် ပြောင်းပါ။

ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဟု ထပ်မံယူဆကြပါစို့။

 import pandas as pd

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

#view DataFrame
df

        team position points rebounds
0 A G 5 11
1 A G 7 8
2 A F 7 10
3 B G 9 6
4 B F 12 6
5 B C 9 5
6 C G 9 9
7 C F 4 12
8 C C 13 10

DataFrame ရှိ categorical variable တစ်ခုစီကို numeric variable အဖြစ်သို့ ပြောင်းလဲရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်ပါသည်။

 #get all categorical columns
cat_columns = df. select_dtypes ([' object ']). columns

#convert all categorical columns to numeric
df[cat_columns] = df[cat_columns]. apply ( lambda x: pd.factorize (x)[ 0 ])

#view updated DataFrame
df

	team position points rebounds
0 0 0 5 11
1 0 0 7 8
2 0 1 7 10
3 1 0 9 6
4 1 1 12 6
5 1 2 9 5
6 2 0 9 9
7 2 1 4 12
8 2 2 13 10

အမှတ်များနှင့် ပြန်လှန်သည့်ကော်လံများသည် တူညီနေသော်လည်း အမျိုးအစားအလိုက် ကော်လံနှစ်ခု (အဖွဲ့နှင့် ရာထူး) နှစ်ခုလုံးကို ဂဏန်းအဖြစ်သို့ ပြောင်းလဲထားကြောင်း သတိပြုပါ။

မှတ်ချက် – pandas factorize() လုပ်ဆောင်ချက်၏ စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာတွေ့နိုင်ပါသည်။

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

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

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

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

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