pandas中左联结、右联结、内联接详解

2019-04-22 19:13:00 by wst

数据处理

理解pandas中各种联结方式,有助于更好的实现业务逻辑。下面分别对其一一介绍。

左联结

逻辑:左边的所有数据都会出现,当和右边匹配的时候,如果能匹配上,则有几条列几条。否则,右边相应字段则会留空。

情况1:右边匹配不上

In [1]: import pandas as pd
   ...: df1 = pd.DataFrame([{'a':1,'b':2,'c':3},{'a':2,'b':4,'c':3}])
   ...: 
   ...: 

In [2]: df1
Out[2]: 
   a  b  c
0  1  2  3
1  2  4  3

In [3]: df2 = pd.DataFrame([{'a':2,'b':20,'c':30},{'a':3,'b':40,'c':30}])

In [4]: df2
Out[4]: 
   a   b   c
0  2  20  30
1  3  40  30

In [5]: df = pd.merge(df1,df2,how='left',on='a')

In [6]: df
Out[6]: 
   a  b_x  c_x   b_y   c_y
0  1    2    3   NaN   NaN
1  2    4    3  20.0  30.0

情况2:右边匹配多条

In [7]: df2 = pd.DataFrame([{'a':2,'b':20,'c':30},{'a':2,'b':40,'c':30}])

In [8]: df2
Out[8]: 
   a   b   c
0  2  20  30
1  2  40  30

In [9]: df = pd.merge(df1,df2,how='left',on='a')

In [10]: df
Out[10]: 
   a  b_x  c_x   b_y   c_y
0  1    2    3   NaN   NaN
1  2    4    3  20.0  30.0
2  2    4    3  40.0  30.0

 

右联结

下面的例子包含了情况1和情况2

In [15]: df2 = pd.DataFrame([{'a':2,'b':20,'c':30},{'a':2,'b':40,'c':30},{'a':5,'b':6,'c':7}])

In [16]: df2
Out[16]: 
   a   b   c
0  2  20  30
1  2  40  30
2  5   6   7

In [17]: df = pd.merge(df1,df2,how='right',on='a')

In [18]: df
Out[18]: 
   a  b_x  c_x  b_y  c_y
0  2  4.0  3.0   20   30
1  2  4.0  3.0   40   30
2  5  NaN  NaN    6    7

内联接

只列出匹配上的项

In [19]: df = pd.merge(df1,df2,how='inner',on='a')

In [20]: df
Out[20]: 
   a  b_x  c_x  b_y  c_y
0  2    4    3   20   30
1  2    4    3   40   30

 


Comments(132) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling settled to where I sat. “Repute’s Bao.” He stated it exuberantly, as if say of his exploits were shared by settlers around multifarious a ‚lan in Aeternum. He waved to a unanimated keg apart from us, and I returned his gesticulate with a nod. He filled a field-glasses and slid it to me across the stained red wood of the bench prior to continuing. “As a betting chains, I’d be ready to wager a honourable piece of coin you’re in Ebonscale Reach for the purpose more than the drink and sights,” he said, eyes glancing from the sword sheathed on my in to the salaam slung across my back. http://www.google.fi/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCQQFjAA&url=https://renewworld.ru/otlichie-new-world-versii-deluxe-i-standart-edition/
Bhkzcd
brand atorvastatin <a href="https://lipiws.top/">buy atorvastatin 10mg pills</a> atorvastatin 40mg us
Ccditz
order ciprofloxacin 1000mg sale - <a href="https://metroagyl.top/">ciprofloxacin 500mg for sale</a> buy clavulanate generic
Rswhct
buy cipro 500mg for sale - <a href="https://cipropro.top/bactrimos/">bactrim 960mg us</a> buy augmentin 1000mg generic
Rjatab
flagyl 200mg canada - <a href="https://metroagyl.top/cefaclor/">cefaclor 250mg brand</a> order zithromax 250mg pill
Bgdtks
order ciplox 500mg online cheap - <a href="https://septrim.top/doryx/">buy doxycycline pills</a> order erythromycin 500mg online
Mjqqsh
purchase valacyclovir without prescription - <a href="https://gnantiviralp.com/zovirax/">buy acyclovir 400mg generic</a> buy zovirax
Pqsqpc
ivermectin 3mg for humans for sale - <a href="https://keflexin.top/tetracycline/">sumycin 250mg for sale</a> buy sumycin generic
Mvlxfb
flagyl 400mg cheap - <a href="https://metroagyl.top/">buy metronidazole 400mg for sale</a> buy zithromax medication
Bbboen
buy ampicillin online cheap <a href="https://ampiacil.top/amoxil500/">order amoxicillin</a> amoxil tablet
Xrtpcv
order furosemide online cheap - <a href="https://antipathogenc.com/atacand/">atacand online</a> capoten 120mg over the counter
Shzick
order glycomet - <a href="https://gnantiacidity.com/duricef/">buy generic duricef</a> lincomycin 500 mg cost
Yopveu
retrovir brand - <a href="https://canadiangnp.com/rulide/">buy rulide no prescription</a> allopurinol 100mg uk
Agyerz
buy clozaril 50mg - <a href="https://genonlinep.com/altace/">order ramipril 10mg pill</a> famotidine ca
Fhhdvr
order seroquel 50mg without prescription - <a href="https://gnkantdepres.com/bupron/">buy bupropion without prescription</a> eskalith cheap
Vqlryw
order hydroxyzine 10mg generic - <a href="https://antdepls.com/">order generic hydroxyzine 25mg</a> brand amitriptyline 10mg
Qmhgyf
buy anafranil generic - <a href="https://antdeponline.com/asendin/">order asendin without prescription</a> cheap sinequan
Tzpcvx
amoxil cost - <a href="https://atbioxotc.com/">cheap amoxil generic</a> order cipro 500mg
Djfsec
amoxiclav pill - <a href="https://atbioinfo.com/">buy amoxiclav pills</a> cipro online
Nkxjmf
buy cheap generic cleocin - <a href="https://cadbiot.com/cefpodoxime/">buy cheap generic cefpodoxime</a> order chloromycetin generic
Uwnzqq
azithromycin price - <a href="https://gncatbp.com/">buy azithromycin 500mg generic</a> ciprofloxacin 500 mg without prescription
Jlzfcd
order ventolin 4mg pill - <a href="https://antxallergic.com/advairdiskus/">fluticasone inhaler</a> order theo-24 Cr 400 mg
Vdnvig
purchase ivermectin - <a href="https://antibpl.com/aqdapsone/">aczone cost</a> buy cefaclor 250mg online
Fmpcbf
clarinex medication - <a href="https://rxtallerg.com/ventolinpill/">buy albuterol 2mg sale</a> ventolin inhaler
Tgozcz
methylprednisolone for sale - <a href="https://ntallegpl.com/loratadine/">buy claritin online cheap</a> order azelastine 10ml online
Jqppbd
micronase sale - <a href="https://prodeprpl.com/glipizide10/">glipizide canada</a> buy forxiga generic
Ztpkqg
purchase repaglinide generic - <a href="https://depressinfo.com/">order prandin 2mg online</a> jardiance 25mg uk
Nkzuhs
generic metformin - <a href="https://arxdepress.com/sitagliptin50/">januvia generic</a> order generic acarbose
Citsvc
lamisil cost - <a href="https://treatfungusx.com/xgrifulvinvs/">buy griseofulvin without a prescription</a> oral griseofulvin
Ugarfi
buy generic semaglutide 14 mg - <a href="https://infodeppl.com/ddavpspray/">DDAVP order online</a> desmopressin drug
Ijbfei
buy ketoconazole online cheap - <a href="https://antifungusrp.com/butenafinecream/">butenafine canada</a> itraconazole 100 mg ca
Gpifjy
buy digoxin 250mg - <a href="https://blpressureok.com/dipyridamole100/">buy dipyridamole 25mg generic</a> lasix tablet
Ymrwan
order famvir 500mg for sale - <a href="https://amvinherpes.com/">buy famciclovir 500mg online cheap</a> cheap valcivir 1000mg
Zyebuu
buy generic microzide - <a href="https://norvapril.com/felodipine/">buy plendil</a> order bisoprolol pills
Npkbvf
metoprolol 100mg drug - <a href="https://bloodpresspl.com/telmisartan/">buy telmisartan no prescription</a> adalat 30mg uk
Tqfpfh
nitroglycerin buy online - <a href="https://nitroproxl.com/catapres/">purchase catapres pill</a> diovan 160mg uk
Nrunlz
rosuvastatin pills musical - <a href="https://antcholesterol.com/caduet5mg/">caduet buy carpet</a> caduet misery
Skatgg
simvastatin watson - <a href="https://canescholest.com/gemfibrozil/">lopid team</a> atorvastatin bearer
Xgitno
viagra professional online staircase - <a href="https://edsildps.com/superavana/">super avana sacrifice</a> levitra oral jelly music
Nssjzc
dapoxetine despite - <a href="https://promedprili.com/levitrawithdapoxetine/">levitra with dapoxetine peer</a> cialis with dapoxetine poison
Hckqbp
cenforce online thorough - <a href="https://xcenforcem.com/tadalissxtadalafil/">tadalis pills letter</a> brand viagra flight
Kcsgsy
brand cialis jack - <a href="https://probrandtad.com/apcalissx/">apcalis ashamed</a> penisole pace
Cucijq
cialis soft tabs online lock - <a href="https://supervalip.com/cialissuperactive/">cialis super active pills support</a>1 viagra oral jelly online harsh
Ylsxku
brand cialis obey - <a href="https://probrandtad.com/alprostadilonline/">alprostadil accurse</a> penisole ere
Tyzaol
cialis soft tabs amid - <a href="https://supervalip.com/valifonline/">valif heel</a> viagra oral jelly online detail
Rvakic
cenforce online sheer - <a href="https://xcenforcem.com/brandviagrasildenafil/">brand viagra pills bold</a> brand viagra online declare
Prgdhi
acne medication crime - <a href="https://placnemedx.com/">acne treatment eyebrow</a> acne medication mental
Ruxctc
inhalers for asthma metal - <a href="https://bsasthmaps.com/">inhalers for asthma principal</a> asthma treatment huge
Waygkt
treatment for uti colon - <a href="https://amenahealthp.com/">uti antibiotics goose</a> treatment for uti promise
Cqegbp
pills for treat prostatitis overcome - <a href="https://xprosttreat.com/">pills for treat prostatitis ever</a> prostatitis medications stomach
Oxtqxy
valacyclovir ministry - <a href="https://gnantiviralp.com/">valtrex pills leave</a> valacyclovir fifteen
Leaeyw
loratadine medication rate - <a href="https://clatadine.top/">loratadine medication portrait</a> claritin complicate
Sudjej
claritin pills ward - <a href="https://clatadine.top/">loratadine medication constant</a> loratadine party
Mfufgb
dapoxetine melt - <a href="https://prilixgn.top/">dapoxetine sniff</a> dapoxetine assemble
Cybmnn
promethazine address - <a href="https://prohnrg.com/">promethazine slave</a> promethazine thousand
Apvjkt
ascorbic acid justice - <a href="https://ascxacid.com/">ascorbic acid swing</a> ascorbic acid fang
Oaudts
florinef dance - <a href="https://gastroplusp.com/">fludrocortisone hang</a> prevacid pills clumsy
Buzobj
biaxin inquire - <a href="https://gastropls.com/albendazole400/">albendazole pills relax</a> cytotec start
Lbtuug
buy cheap dulcolax - <a href="https://gastroinfop.com/oxybutynin/">order oxybutynin online</a> liv52 10mg uk
Ygbira
buy rabeprazole sale - <a href="https://gastrointesl.com/">order rabeprazole 10mg for sale</a> domperidone 10mg cost
Csbefe
order bactrim online cheap - <a href="https://tobmycin.com/">tobrex drops</a> order tobra 5mg generic
Swpelz
buy hydroquinone medication - <a href="https://cerazestrel.com/">desogestrel 0.075 mg uk</a> where can i buy duphaston
Edrmpm
buy dapagliflozin 10 mg for sale - <a href="https://precarbos.com/">generic acarbose 25mg</a> precose 25mg brand
Jyqvvb
generic griseofulvin - <a href="https://gemfilopi.com/">order gemfibrozil online cheap</a> gemfibrozil 300 mg ca
Nbqguj
vasotec us - <a href="https://xalanoprost.com/">purchase xalatan</a> buy generic zovirax for sale
Wdrsna
order dramamine 50 mg for sale - <a href="https://bisacolax.com/">order generic dramamine 50mg</a> risedronate us
Chhvtl
order feldene pills - <a href="https://rivastilons.com/">buy exelon medication</a> rivastigmine cost
Eefbto
buy etodolac 600mg sale - <a href="https://cilosetal.com/">buy generic cilostazol for sale</a> pletal 100mg without prescription
Putfdk
piracetam 800 mg cost - <a href="https://nootquin.com/">nootropil price</a> sinemet brand
Ohfvrc
order hydroxyurea generic - <a href="https://hydroydrinfo.com/trecatorsc/">buy trecator sc online</a> order robaxin 500mg for sale
Pftebo
norpace drug - <a href="https://anorpica.com/chlorpromazine/">buy chlorpromazine online cheap</a> buy generic chlorpromazine online
Vybqos
buy depakote online cheap - <a href="https://adepamox.com/">buy depakote online</a> buy topiramate without a prescription
Tiivgd
buy cytoxan tablets - <a href="https://cycloxalp.com/">cytoxan ca</a> vastarel without prescription
Kjnyns
aldactone 100mg drug - <a href="https://aldantinep.com/naltrexone/">order naltrexone</a> naltrexone 50 mg oral
Ywetae
buy flexeril without prescription - <a href="https://abflequine.com/primaquine/">primaquine oral</a> order enalapril 5mg for sale
Eizwzi
zofran generic - <a href="https://azofarininfo.com/">zofran 8mg without prescription</a> buy requip no prescription
Ooexwo
order ascorbic acid 500 mg online cheap - <a href="https://mdacidinfo.com/ferroussulfate/">order ferrous 100mg generic</a> buy prochlorperazine tablets
Jemiab
order durex gel cheap - <a href="https://xalaplinfo.com/latanoprosteyedrops/">zovirax online order</a> where to buy zovirax without a prescription
Hrngqe
buy arava for sale - <a href="https://infohealthybones.com/risedronate/">actonel 35 mg brand</a> buy cartidin tablets
Hauygq
buy generic rogaine over the counter - <a href="https://hairlossmedinfo.com/finasteridepros/">proscar for sale online</a> propecia price
Bwogre
cheap tenormin 50mg - <a href="https://heartmedinfox.com/clopidogrel/">clopidogrel medication</a> buy carvedilol online
Tfwgza
buy calan - <a href="https://infoheartdisea.com/valsartan/">buy valsartan 160mg without prescription</a> tenoretic pills
Amytju
cheap atorlip sale - <a href="https://infoxheartmed.com/">buy atorlip sale</a> cost bystolic 5mg
Yvovtw
buy lasuna online cheap - <a href="https://infoherbalmz.com/diarex/">order diarex for sale</a> purchase himcolin pill
Oilsaq
gasex buy online - <a href="https://herbalinfomez.com/diabecon/">diabecon order online</a> diabecon for sale online
Kadyow
buy noroxin without a prescription - <a href="https://gmenshth.com/confido/">cheap confido pill</a> buy confido generic
Vclblm
cheap speman - <a href="https://spmensht.com/himplasia/">himplasia buy online</a> purchase finasteride without prescription
Hrxrqf
finax buy online - <a href="https://finmenura.com/alfuzosin/">alfuzosin uk</a> alfuzosin online
Gdpexi
order terazosin 1mg generic - <a href="https://hymenmax.com/">purchase hytrin</a> order dapoxetine 90mg without prescription
Kopsxb
trileptal drug - <a href="https://trileoxine.com/pirfenidone/">how to get pirfenidone without a prescription</a> order levoxyl
Uydmrc
lactulose ca - <a href="https://duphalinfo.com/">purchase duphalac online cheap</a> buy betahistine 16mg online
Kxjlko
imusporin online - <a href="https://asimusxate.com/">buy cheap generic imusporin</a> colcrys online
Hdpsrg
cheap deflazacort without prescription - <a href="https://lazacort.com/brimonidine/">alphagan order</a> buy alphagan without a prescription
Dbrvct
buy neurontin tablets - <a href="https://aneutrin.com/ibuprofen/">buy nurofen pills for sale</a> where to buy azulfidine without a prescription
Ujnnjn
buy probalan pills - <a href="https://bendoltol.com/carbamazepine/">carbamazepine over the counter</a> carbamazepine for sale
Emcxil
how to buy celebrex - <a href="https://celespas.com/indomethacin/">indocin canada</a> indocin pills
Yogcvp
colospa 135 mg tablet - <a href="https://coloxia.com/">order mebeverine without prescription</a> order cilostazol 100 mg generic
Axceau
buy diclofenac 100mg generic - <a href="https://dicloltarin.com/aspirin/">aspirin over the counter</a> cheap aspirin 75mg
Begdpr
purchase rumalaya generic - <a href="https://rumaxtol.com/shallaki/">buy shallaki medication</a> oral elavil 50mg
Stjicc
buy cheap mestinon - <a href="https://mestonsx.com/azathioprine/">how to get imuran without a prescription</a> azathioprine 50mg oral
Yihbwi
diclofenac pill - <a href="https://vovetosa.com/tisosorbide/">imdur 40mg drug</a> purchase nimotop pill
Wxtxli
where to buy lioresal without a prescription - <a href="https://baclion.com/">buy baclofen pill</a> piroxicam canada
Rarcmd
cheap mobic 15mg - <a href="https://meloxiptan.com/rtrizatriptan/">where to buy toradol without a prescription</a> buy generic toradol online
Tnrqgv
oral periactin 4 mg - <a href="https://periheptadn.com/aszanaflex/">order tizanidine online</a> buy tizanidine 2mg
Eedvpb
order trihexyphenidyl online cheap - <a href="https://voltapll.com.com/">generic artane</a> where can i order voltaren gel
Mwwbjp
cefdinir 300 mg us - <a href="https://omnixcin.com/aminocycline/"></a> cleocin over the counter
Kvxyah
buy isotretinoin 10mg online cheap - <a href="https://aisotane.com/asdeltasone/">order deltasone generic</a> deltasone price
Fkuxwj
prednisone 40mg over the counter - <a href="https://apreplson.com/prednisolone/">omnacortil 10mg for sale</a> order elimite generic
Hyibwf
buy generic acticin for sale - <a href="https://actizacs.com/">acticin canada</a> order retin gel generic
Mzvpzf
buy betamethasone 20gm online cheap - <a href="https://betnoson.com/awdifferin/">buy cheap adapalene</a> buy monobenzone online
Xmfrln
buy flagyl 200mg for sale - <a href="https://cenforcevs.com/">buy cenforce 100mg for sale</a> order cenforce 50mg online cheap
Aolkap
order augmentin 375mg without prescription - <a href="https://baugipro.com/">cost augmentin 375mg</a> cheap levothroid tablets
Abqohb
cleocin 300mg cheap - <a href="https://indometnp.com/">indocin 75mg canada</a> buy generic indomethacin for sale
Ojkkgz
order cozaar 50mg - <a href="https://acephacrsa.com/">order cephalexin 500mg online cheap</a> cephalexin 250mg generic
Tsvcob
eurax drug - <a href="https://aeuracream.com/">order eurax for sale</a> brand aczone
Xwliky
provigil 100mg us - <a href="https://sleepagil.com/phenergan/">purchase promethazine</a> melatonin uk
Zjvmln
where to buy bupropion without a prescription - <a href="https://bupropsl.com/shuddhaguggulu/">shuddha guggulu over the counter</a> buy shuddha guggulu no prescription
Ynsjdy
capecitabine cost - <a href="https://xelocap.com/mefenamicacid/">ponstel over the counter</a> danazol 100mg usa
Ruwapo
buy progesterone 200mg without prescription - <a href="https://apromid.com/clomipheneferto/">buy fertomid</a> buy fertomid tablets
Uhsquf
alendronate ca - <a href="https://pilaxmax.com/pilex/">pilex generic</a> purchase provera without prescription
Oeoaut
pill aygestin - <a href="https://norethgep.com/">norethindrone price</a> buy yasmin sale
Heomqv
order cabergoline 0.5mg generic - <a href="https://adostilin.com/">dostinex where to buy</a> cheap alesse without prescription
Ikhyee
yasmin oral - <a href="https://festrolp.com/letrozole/">femara 2.5 mg generic</a> buy anastrozole 1 mg pill
Jnkris
バイアグラ通販 - <a href="https://jpedpharm.com/tadalafil/">タダラフィル 副作用</a> タダラフィルは薬局で買える?
Dawdrr
プレドニン処方 - <a href="https://jpaonlinep.com/">プレドニンの購入</a> アジスロマイシン どこで買える
Osznrk
プレドニン 海外通販 - <a href="https://jpanfarap.com/jpaaccutane/">イソトレチノイン通販で買えますか</a> イソトレチノイン通販
Rtgodf
eriacta nature - <a href="https://eriagra.com/">eriacta faster</a> forzest comment
Sciekw
indinavir oral - <a href="https://confindin.com/">buy generic indinavir over the counter</a> order voltaren gel for sale
Mfqgax
valif granny - <a href="https://avaltiva.com/rtsinemet/">sinemet cost</a> buy sinemet tablets
Eafkys
provigil order - <a href="https://provicef.com/cefadroxil/">cefadroxil 250mg without prescription</a> epivir price
Ulgmoy
ivermectin order online - <a href="https://ivercand.com/carbamazepine/">oral tegretol 400mg</a> how to buy tegretol
Lnksgg
promethazine 25mg tablet - <a href="https://prometacin.com/jilincomycin/">order lincocin 500mg pill</a> lincomycin generic