Pandas ရှိ အညွှန်းသို့ ကော်လံသို့ မည်သို့ပြောင်းနည်း (ဥပမာများဖြင့်)


Pandas DataFrame ၏ အညွှန်းကိန်းတစ်ခုကို ကော်လံတစ်ခုသို့ ပြောင်းရန် အောက်ပါအခြေခံ syntax ကို သင်အသုံးပြုနိုင်သည်-

 #convert index to column
df. reset_index (inplace= True )

သင့်တွင် Pandas MultiIndex DataFrame ရှိပါက၊ သင်သည် အညွှန်းကိန်း၏ သီးခြားအဆင့်ကို ကော်လံတစ်ခုသို့ ပြောင်းရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်သည်။

 #convert specific level of MultiIndex to column
df. reset_index (inplace= True ,level=[' Level1 '])

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

ဥပမာ 1- အညွှန်းကို ကော်လံအဖြစ် ပြောင်းပါ။

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

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' points ': [25, 12, 15, 14, 19],
                   ' assists ': [5, 7, 7, 9, 12],
                   ' rebounds ': [11, 8, 10, 6, 6]})

#view DataFrame
df

points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6

#convert index to column
df. reset_index (inplace= True )

#view updated DataFrame
df

	index points assists rebounds
0 0 25 5 11
1 1 12 7 8
2 2 15 7 10
3 3 14 9 6
4 4 19 12 6

ဥပမာ 2- MultiIndex ကို ကော်လံများအဖြစ် ပြောင်းပါ။

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

 import pandas as pd

#createDataFrame
index_names = pd. MultiIndex . from_tuples ([('Level1','Lev1', 'L1'),
                                       ('Level2','Lev2', 'L2'),
                                       ('Level3','Lev3', 'L3'),
                                       ('Level4','Lev4', 'L4')],
                                       names=['Full','Partial', 'ID'])

data = {' Store ': ['A','B','C','D'],
        ' Sales ': [17, 22, 29, 35]}

df = pd. DataFrame (data, columns = [' Store ',' Sales '], index=index_names)

#view DataFrame
df

                    Store Sales
Full Partial ID		
Level1 Lev1 L1 A 17
Level2 Lev2 L2 B 22
Level3 Lev3 L3 C 29
Level4 Lev4 L4 D 35

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

 #convert all levels of index to columns
df. reset_index (inplace= True )

#view updated DataFrame
df

        Full Partial ID Store Sales
0 Level1 Lev1 L1 A 17
1 Level2 Lev2 L2 B 22
2 Level3 Lev3 L3 C 29
3 Level4 Lev4 L4 D 35

MultiIndex အဆင့်ကိုသာ ကော်လံတစ်ခုသို့ ပြောင်းရန် အောက်ပါကုဒ်ကိုလည်း အသုံးပြုနိုင်သည်။

 #convert just 'ID' index to column in DataFrame
df. reset_index (inplace= True ,level=[' ID '])

#view updated DataFrame
df

		ID Store Sales
Full Partial			
Level1 Lev1 L1 A 17
Level2 Lev2 L2 B 22
Level3 Lev3 L3 C 29
Level4 Lev4 L4 D 35

“ ID” အဆင့်ကိုသာ DataFrame ရှိ ကော်လံတစ်ခုအဖြစ်သို့ ပြောင်းလဲထားကြောင်း သတိပြုပါ။

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

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

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

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

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