ပန်ဒါများ- ကိန်းသေတန်ဖိုးတစ်ခုဖြင့် ကော်လံတစ်ခုထည့်နည်း


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

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

 df[' new '] = 5

နည်းလမ်း 2- တူညီသောကိန်းသေတန်ဖိုးဖြင့် ကော်လံများစွာကို ထည့်ပါ။

 df[[' new1 ', ' new2 ', ' new3 ']] = 5

နည်းလမ်း 3- မတူညီသော အဆက်မပြတ်တန်ဖိုးများဖြင့် ကော်လံအများအပြားကို ထည့်ပါ။

 #define dictionary of new values
new_constants = {' new1 ':5,' new2 ':10,' new3 ':15}

#add multiple columns with different constant values
df = df. assign ( ** new_constants)

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

 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]})

#view DataFrame
print (df)

  team points assists
0 to 18 5
1 B 22 7
2 C 19 7
3 D 14 9
4 E 14 12
5 F 11 9
6 G 20 9
7:28 a.m. 4

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

အောက်ပါကုဒ်သည် အတန်းတစ်ခုစီအတွက် 5 တန်ဖိုးရှိသော ကော်လံတစ်ခုကို မည်သို့ထည့်ရမည်ကို ပြသသည်-

 #add column with constant value
df[' new '] = 5

#view updated DataFrame
print (df)

  team points assists new
0 to 18 5 5
1 B 22 7 5
2 C 19 7 5
3 D 14 9 5
4 E 14 12 5
5 F 11 9 5
6 G 20 9 5
7:28 4 5

အသစ်ဟုခေါ်သော ကော်လံ အသစ်ကို အတန်းတစ်ခုစီအတွက် ကိန်းသေတန်ဖိုး 5 ဖြင့် ဖြည့်ထားသည်။

ဥပမာ 2- တူညီသောကိန်းသေတန်ဖိုးဖြင့် ကော်လံများစွာကို ထည့်ပါ။

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

 #add three new columns each with a constant value of 5
df[[' new1 ', ' new2 ', ' new3 ']] = 5

#view updated DataFrame
print (df)

  team points assists new1 new2 new3
0 to 18 5 5 5 5
1 B 22 7 5 5 5
2 C 19 7 5 5 5
3 D 14 9 5 5 5
4 E 14 12 5 5 5
5 F 11 9 5 5 5
6 G 20 9 5 5 5
7:28 A.M. 4 5 5 5

ကော်လံအသစ်တစ်ခုစီတွင် အတန်းတစ်ခုစီတွင် တန်ဖိုး 5 ပါဝင်ကြောင်း သတိပြုပါ။

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

အောက်ပါကုဒ်သည် မတူညီသော ကိန်းသေတန်ဖိုးများဖြင့် ကော်လံအများအပြားကို ထည့်သွင်းနည်းကို ပြသသည်-

 #define dictionary of new values
new_constants = {' new1 ':5,' new2 ':10,' new3 ':15}

#add multiple columns with different constant values
df = df. assign ( ** new_constants)

#view updated DataFrame
print (df)

  team points assists new1 new2 new3
0 A 18 5 5 10 15
1 B 22 7 5 10 15
2 C 19 7 5 10 15
3 D 14 9 5 10 15
4 E 14 12 5 10 15
5 F 11 9 5 10 15
6 G 20 9 5 10 15
7:28 a.m. 4 5 10 15

ကော်လံအသစ်သုံးခုမှ တစ်ခုစီတွင် မတူညီသော ကိန်းသေတန်ဖိုးများ ရှိနေကြောင်း သတိပြုပါ။

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

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

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

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

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