Pandas- dataframe တစ်ခုမှအတန်းတစ်ခုရှိမရှိ စစ်ဆေးပါ။


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

 #merge two DataFrames on specific columns
all_df = pd. merge (df1, df2, on=[' column1 ', ' column2 '], how=' left ', indicator=' exists ')

#drop unwanted columns
all_df = all_df. drop (' column3 ', axis= 1 )

#add column that shows if each row in one DataFrame exists in another
all_df[' exists '] = np. where (all_df. exists == ' both ', True , False )

အောက်ဖော်ပြပါ ဥပမာသည် ဤ syntax ကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ပြသထားသည်။

ဥပမာ- Pandas DataFrame တစ်ခုရှိ အတန်းတစ်ခုရှိမရှိ စစ်ဆေးပါ။

ကျွန်ုပ်တို့တွင် အောက်ပါ Panda DataFrames နှစ်ခုရှိသည်ဟု ယူဆကြပါစို့။

 import pandas as pd

#create first DataFrame
df1 = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E'], 
                    ' points ': [12, 15, 22, 29, 24]}) 

print (df1)

  team points
0 to 12
1 B 15
2 C 22
3 D 29
4 E 24

#create second DataFrame
df2 = pd. DataFrame ({' team ': ['A', 'D', 'F', 'G', 'H'],
                    ' points ': [12, 29, 15, 19, 10],
                    ' assists ': [4, 7, 7, 10, 12]})

print (df2)

  team points assists
0 to 12 4
1 D 29 7
2 F 15 7
3 G 19 10
4:10:12

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

 import numpy as np

#merge two dataFrames and add indicator column
all_df = pd. merge (df1, df2, on=[' team ', ' points '], how=' left ', indicator=' exists ')

#drop assists columns
all_df = all_df. drop (' assists ', axis= 1 )

#add column to show if each row in first DataFrame exists in second
all_df[' exists '] = np. where (all_df. exists == ' both ', True , False )

#view updated DataFrame
print (all_df)

  team points exists
0 A 12 True
1 B 15 False
2 C 22 False
3 D 29 True
4 E 24 False

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

ရလဒ်မှ ကျွန်ုပ်တို့ မြင်နိုင်သည်-

  • A ၏ အသင်းတန်ဖိုးနှင့် အမှတ်တန်ဖိုး 12 သည် ဒုတိယ DataFrame တွင် ရှိနေသည်။
  • အဖွဲ့တန်ဖိုး B နှင့် 15 အမှတ်တန်ဖိုးသည် ဒုတိယ DataFrame တွင်မရှိပါ။
  • အဖွဲ့တန်ဖိုး C နှင့် အမှတ်တန်ဖိုး 22 သည် ဒုတိယ DataFrame တွင် မရှိပါ။
  • D ၏ အဖွဲ့တန်ဖိုးနှင့် အမှတ်တန်ဖိုး 29 သည် ဒုတိယ DataFrame တွင် ရှိနေသည်။
  • E ၏ အသင်းတန်ဖိုးနှင့် အမှတ်တန်ဖိုး 24 သည် ဒုတိယ DataFrame တွင် မရှိပါ။

NumPy Where() လုပ်ဆောင်ချက်ရှိ တန်ဖိုးများကို ပြောင်းလဲခြင်းဖြင့် လက်ရှိ ကော်လံတွင် True နှင့် False မှလွဲ၍ အခြားတန်ဖိုးများကို သတ်မှတ်နိုင်သည်ကို သတိပြုပါ။

ဥပမာအားဖြင့်၊ သင်သည် အောက်ပါအတိုင်း “ exists” နှင့် “ not exist” ကိုသုံးနိုင်သည်။

 #add column to show if each row in first DataFrame exists in second
all_df[' exists '] = np. where (all_df. exists == ' both ', ' exists ', ' not exists ')

#view updated DataFrame
print (all_df)

  team points exists
0 to 12 exists
1 B 15 not exists
2 C 22 not exists
3 D 29 exists
4 E 24 not exists

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

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

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

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

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

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