Pandas dataframe တွင် ကော်လံအစီစဥ်ကို မည်သို့ပြောင်းရမည်နည်း။


Pandas DataFrame တွင် ကော်လံများ၏ အစီအစဥ်ကို လျင်မြန်စွာပြောင်းလဲရန် အောက်ပါ syntax ကို သင်အသုံးပြုနိုင်သည်-

 df[[' column2 ', ' column3 ', ' column1 ']]

အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် ဤ syntax ကိုမည်သို့အသုံးပြုရမည်ကိုပြသသည်-

 import pandas as pd

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

#display 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
5 23 9 5
6 25 9 9
7 29 4 12

ဥပမာ 1- ကော်လံများ၏ အစီအစဥ်ကို အမည်ဖြင့် ပြောင်းလဲပါ။

အောက်ပါကုဒ်သည် အမည်ကို အခြေခံ၍ DataFrame ရှိ ကော်လံများ၏ အစီအမံများကို မည်သို့ပြောင်းလဲရမည်ကို ပြသသည်-

 #change order of columns by name
df[[' rebounds ', ' assists ', ' points ']]

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

ဥပမာ 2- ပထမကော်လံအသစ်တစ်ခုထည့်ခြင်းဖြင့် အမှာစာကိုပြောင်းပါ။

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

 #define new column to add
steals = [2, 3, 3, 4, 3, 2, 1, 2]

#insert new column in first position
df. insert (0, ' steals ', steals)

#display dataFrame
df
        steals points assists rebounds
0 2 25 5 11
1 3 12 7 8
2 3 15 7 10
3 4 14 9 6
4 3 19 12 6
5 2 23 9 5
6 1 25 9 9
7 2 29 4 12

ဥပမာ 3- နောက်ဆုံးကော်လံအသစ်တစ်ခုကို ထည့်ခြင်းဖြင့် အမှာစာကို ပြောင်းပါ။

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

 #define new column to add
steals = [2, 3, 3, 4, 3, 2, 1, 2]

#insert new column in last position
df. insert ( len ( df.columns ), ' steals ', steals)

#display dataFrame
df

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

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

Pandas DataFrame တွင် ကော်လံတစ်ခု ထည့်သွင်းနည်း
Pandas ရှိ အညွှန်းကော်လံကို ဖယ်ရှားနည်း
Pandas တွင် ကော်လံနှစ်ခုကို ပေါင်းစပ်နည်း

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

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