Pandas- dataframe ရှေ့မှာ ကော်လံတစ်ခုကို ဘယ်လိုရွှေ့မလဲ။
ပန်ဒါ DataFrame တွင် ကော်လံများကို ရှေ့သို့ရွှေ့ရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
နည်းလမ်း 1- ကော်လံတစ်ခုကို ရှေ့သို့ရွှေ့ပါ။
df = df[[' my_col '] + [x for x in df. columns if x != ' my_col ']]
နည်းလမ်း 2- ကော်လံအများအပြားကို ရှေ့သို့ရွှေ့ပါ။
cols_to_move = [' my_col1 ', ' my_col2 '] df = df[cols_to_move + [x for x in df. columns if x not in cols_to_move]]
အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-
import pandas as pd
#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
' points ': [18, 22, 19, 14, 14, 11, 20, 28],
' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})
#view DataFrame
print (df)
team points assists rebounds
0 A 18 5 11
1 B 22 7 8
2 C 19 7 10
3 D 14 9 6
4 E 14 12 6
5 F 11 9 5
6 G 20 9 9
7:28 4 12
ဥပမာ 1- ကော်လံတစ်ခုကို ရှေ့သို့ရွှေ့ပါ။
အောက်ပါကုဒ်သည် “ ကူညီသူများ” ကော်လံကို DataFrame ၏အရှေ့ဘက်သို့ မည်သို့ရွှေ့ရမည်ကို ပြသသည်-
#move 'assists' column to front
df = df[[' assists '] + [x for x in df. columns if x != ' assists ']]
#view updated DataFrame
print (df)
assists team points rebounds
0 5 A 18 11
1 7 B 22 8
2 7 C 19 10
3 9 D 14 6
4 12 E 14 6
5 9 F 11 5
6 9 G 20 9
7 4 H 28 12
“ ပံ့ပိုးမှု” ကော်လံကို DataFrame ၏အရှေ့ဘက်သို့ ရွှေ့လိုက်ပြီး အခြားကော်လံများအားလုံးသည် အစဉ်လိုက်အတိုင်း ကျန်ရှိနေပါသည်။
ဥပမာ 2- ကော်လံအများအပြားကို ရှေ့သို့ရွှေ့ပါ။
အောက်ဖော်ပြပါကုဒ်သည် DataFrame ၏အရှေ့ဘက်သို့ “ points” နှင့် “ bounces” ကော်လံများကို မည်သို့ရွှေ့ရမည်ကို ပြသသည်-
#define columns to move to front
cols_to_move = [' points ', ' rebounds ']
#move columns to front
df = df[cols_to_move + [x for x in df. columns if x not in cols_to_move]]
#view updated DataFrame
print (df)
points rebounds team assists
0 18 11 A 5
1 22 8 B 7
2 19 10 C 7
3 14 6 D 9
4 14 6 E 12
5 11 5 F 9
6 20 9 G 9
7 28 12 H 4
“points” နှင့် “bounces” ကော်လံနှစ်ခုလုံးကို DataFrame ၏အရှေ့ဘက်သို့ ရွှေ့ထားသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ပန်ဒါများတွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Pandas DataFrame တွင် ကော်လံတစ်ခုကို ထည့်သွင်းနည်း
Pandas ရှိ အညွှန်းကော်လံကို ဖယ်ရှားနည်း
Pandas တွင် ကော်လံနှစ်ခုကို ပေါင်းစပ်နည်း