Pandas- dataframe တစ်ခုမှ အခြားကော်လံတစ်ခုကို မည်သို့ထည့်မည်နည်း။


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

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

 #add some_col from df2 to last column position in df1
df1[' some_col ']= df2[' some_col ']

နည်းလမ်း 2- DataFrame တစ်ခု၏ကော်လံတစ်ခုကို အခြားတစ်ခုရှိ သီးခြားအနေအထားတစ်ခုတွင် ထည့်ပါ။

 #insert some_col from df2 into third column position in df1
df1. insert ( 2 , ' some_col ', df2[' some_col '])

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

 import pandas as pd

#create first DataFrame
df1 = pd. DataFrame ({'team': ['A', 'A', 'A', 'A', 'B', 'B'],
                    'position': ['G', 'G', 'F', 'C', 'G', 'C'],
                    'points': [4, 4, 6, 8, 9, 5]})

#view DataFrame
print (df1)

  team position points
0 AG 4
1 GA 4
2 AF 6
3 AC 8
4 BG 9
5 BC 5

#create second DataFrame
df2 = pd. DataFrame ({'team': ['A', 'A', 'A', 'A', 'B', 'B'],
                    'rebounds': [12, 7, 8, 8, 5, 11]})

#view DataFrame
print (df2)

  team rebounds
0 to 12
1 to 7
2 to 8
3 to 8
4 B 5
5 B 11

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

အောက်ပါကုဒ်သည် ဒုတိယ DataFrame ၏ ဘောင်ဝင်သော ကော်လံကို ပထမ DataFrame ၏ နောက်ဆုံးကော်လံအနေအထားသို့ မည်သို့ထည့်ရမည်ကို ပြသသည်-

 #add rebounds column from df2 to df1
df1[' rebounds ']= df2[' rebounds ']

#view updated DataFrame
print (df1)

  team position points rebounds
0 AG 4 12
1 GA 4 7
2 AF 6 8
3 AC 8 8
4 BG 9 5
5 BC 5 11

ဒုတိယ DataFrame ၏ အပြန်အလှန် ကော်လံကို ပထမ DataFrame ၏ နောက်ဆုံးကော်လံအနေအထားသို့ ထည့်ထားကြောင်း သတိပြုပါ။

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

အောက်ဖော်ပြပါ ကုဒ်သည် ဒုတိယ DataFrame ၏ ဘောင်ဝင်သည့် ကော်လံကို ပထမ DataFrame ၏ တတိယကော်လံအနေအထားသို့ မည်သို့ထည့်ရမည်ကို ပြသသည်-

 #insert rebounds column from df2 into third column position of df1
df1. insert ( 2 , ' rebounds ', df2[' rebounds '])

#view updated DataFrame
print (df1)

  team position rebounds points
0 AG 12 4
1 GA 7 4
2 AF 8 6
3 AC 8 8
4 BG 5 9
5 BC 11 5

ဒုတိယ DataFrame ၏ အပြန်အလှန် ကော်လံကို ပထမ DataFrame ၏ တတိယကော်လံအနေအထားသို့ ထည့်ထားကြောင်း သတိပြုပါ။

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

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

Pandas တွင် ကော်လံအစီစဥ်ကို ပြောင်းလဲနည်း
Pandas တွင် ကော်လံအမည်ပြောင်းနည်း
Pandas တွင် ကော်လံများကို အမည်ခွဲနည်း

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

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