Pandas- ကော်လံအမည်များတွင် ရှေ့ဆက်ထည့်နည်း


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

နည်းလမ်း 1- ကော်လံအမည်များအားလုံးတွင် ရှေ့ဆက်တစ်ခုထည့်ပါ။

 df = df. add_prefix (' my_prefix_ ')

နည်းလမ်း 2- သတ်မှတ်ထားသော ကော်လံအမည်များသို့ ရှေ့ဆက်တစ်ခု ထည့်ပါ။

 #specify columns to add prefix to
cols = [' col1 ', ' col3 ']

#add prefix to specific columns
df = df. rename (columns={c: ' my_prefix_ '+c for c in df. columns if c in cols})

အောက်ပါနမူနာများသည် အောက်ပါ pandas DataFrame ဖြင့် ဤနည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-

 import pandas as pd

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

#view DataFrame
print (df)

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

နည်းလမ်း 1- ကော်လံအမည်များအားလုံးတွင် ရှေ့ဆက်တစ်ခုထည့်ပါ။

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

 #add 'total_' as prefix to each column name
df = df. add_prefix (' total_ ')

#view updated DataFrame
print (df)

   total_points total_assists total_rebounds total_blocks
0 25 5 11 6
1 12 7 8 6
2 15 7 10 3
3 14 9 6 2
4 19 12 6 7
5 23 9 5 9

ရှေ့ဆက် “ _total” ကို ကော်လံအမည်များအားလုံးတွင် ထည့်ထားကြောင်း သတိပြုပါ။

နည်းလမ်း 2- သတ်မှတ်ထားသော ကော်လံအမည်များသို့ ရှေ့ဆက်တစ်ခု ထည့်ပါ။

အောက်ပါကုဒ်တွင် “ total_” ၏ရှေ့ဆက်ကို အမှတ်များ သာမက ကော်လံများတွင် အထောက်အကူပြုပုံ ကို ဖော်ပြသည်-

 #specify columns to add prefix to
cols = [' points ', ' assists ']

#add _'total' as prefix to specific columns
df = df. rename (columns={c: ' total_ '+c for c in df.columns if c in cols})

#view updated DataFrame
print (df)

   total_points total_assists rebounds blocks
0 25 5 11 6
1 12 7 8 6
2 15 7 10 3
3 14 9 6 2
4 19 12 6 7
5 23 9 5 9

‘total_’ ၏ရှေ့ဆက်ကို အမှတ်များ နှင့် အထောက်အကူပြု ကော်လံများသို့သာ ထည့်သွင်းထားကြောင်း သတိပြုပါ။

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

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

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

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

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