Pandas- ရာခိုင်နှုန်းများဖြင့် pivottable ဖန်တီးနည်း


သတ်မှတ်ထားသောကော်လံအတွက် စုစုပေါင်း၏ရာခိုင်နှုန်းကိုပြသသည့် ပန်ဒါများရှိ ဆုံချက်ဇယားတစ်ခုသို့ ကော်လံတစ်ခုထည့်ရန် အောက်ပါအထားအသိုကိုသုံးနိုင်သည်။

 my_table[' % points '] = (my_table[' points ']/my_table[' points ']. sum ())* 100

ဤအထူးအစီအစဥ်သည် အမှတ်များကော်လံရှိ စုစုပေါင်း တန်ဖိုးများ၏ ရာခိုင်နှုန်းများကိုပြသသည့် my_table ဟုခေါ်သော pivot ဇယားတစ်ခုသို့ % အမှတ် ဟုခေါ်သော ကော်လံအသစ်ကို ပေါင်းထည့်သည်။

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

ဥပမာ- ရာခိုင်နှုန်းများဖြင့် Pandas PivotTable တစ်ခုကို ဖန်တီးပါ။

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

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   ' position ': ['Guard', 'Guard', 'Forward', 'Forward',
                                'Guard', 'Guard', 'Forward', 'Forward'],
                   ' points ': [22, 30, 14, 15, 19, 30, 23, 20]})

#view DataFrame
print (df)

  team position points
0 A Guard 22
1A Guard 30
2 A Forward 14
3 A Forward 15
4 B Guard 19
5 B Guard 30
6 B Forward 23
7 B Forward 20

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

 #create pivot table to calculate sum of points by team and position
my_table = pd. pivot_table (df, index=[' team ', ' position '], aggfunc=' sum ')

#view pivot table
print (my_table)

               points
team position        
A Forward 29
     Guard 52
B Forward 43
     Guard 49

ရလဒ်မှ ကျွန်ုပ်တို့ မြင်နိုင်သည်-

  • အသင်း A ရဲ့ တိုက်စစ်သမားတွေက စုစုပေါင်း ၂၉ မှတ် ရခဲ့ပါတယ်။
  • အသင်း A ရဲ့ အစောင့်တွေက စုစုပေါင်း 52 မှတ် ရခဲ့ပါတယ်။
  • Team B ၏ တိုက်ခိုက်သူများသည် စုစုပေါင်း 43 မှတ်ရခဲ့သည်။
  • Team B ရဲ့ အစောင့်တွေက စုစုပေါင်း 49 မှတ် ရခဲ့ပါတယ်။

ထို့နောက် အတန်းတစ်ခုစီအတွက် စုစုပေါင်းရမှတ်များ၏ ရာခိုင်နှုန်းကိုပြသသည့် % points ဟုခေါ်သော ကော်လံအသစ်တစ်ခုကို ထည့်ရန် အောက်ပါ syntax ကိုသုံးနိုင်သည်-

 #add column that displays points as a percentage of total points
my_table[' % points '] = (my_table[' points ']/my_table[' points ']. sum ())* 100

#view updated pivot table
print (my_table)

               points % points
team position                   
A Forward 29 16.763006
     Guard 52 30.057803
B Forward 43 24.855491
     Guard 49 28.323699

% Points ကော်လံအသစ်သည် ယခု အမှတ်တန်ဖိုးများကို စုစုပေါင်းရမှတ်များ၏ ရာခိုင်နှုန်းတစ်ခုအဖြစ် ပြသသည်။

ရာခိုင်နှုန်းတန်ဖိုးများကို အချို့ဒဿမနေရာများသို့ လှည့်ရန် round() လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်ကြောင်းကိုလည်း သတိပြုပါ။

 #add column that displays points as a percentage of total points (rounded)
my_table[' % points '] = round ((my_table[' points ']/my_table[' points ']. sum ())* 100 , 2 )

#view updated pivot table
print (my_table)

               points % points
team position                  
A Forward 29 16.76
     Guard 52 30.06
B Forward 43 24.86
     Guard 49 28.32

ရာခိုင်နှုန်းတန်ဖိုးများကို ယခုအခါ ဒဿမနေရာနှစ်ခုသို့ ဝိုင်းထားသည်။

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

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

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

Pandas- ဆုံချက်ဇယားသို့ စစ်ထုတ်နည်းထည့်နည်း
Pandas- ကော်လံရှိ တန်ဖိုးများအလိုက် pivot ဇယားကို ခွဲနည်း
Pandas- တန်ဖိုးများပေါင်းစုထားသော pivot table တစ်ခုကို ဖန်တီးနည်း

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

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