{"id":605,"date":"2017-05-05T14:34:28","date_gmt":"2017-05-05T06:34:28","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=605"},"modified":"2017-05-05T14:34:28","modified_gmt":"2017-05-05T06:34:28","slug":"pythonpandas%e4%b8%ad%e7%9a%84join%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=605","title":{"rendered":"[python]Pandas\u4e2d\u7684join\u65b9\u5f0f"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,314<\/span><span class='epvc-label'> Views<\/span><\/div><p>\u8fde\u8868\u67e5\u8be2\u65b9\u5f0f\u5171\u5305\u62ec4\u79cd\uff1ainner\u3001left\u3001right\u3001outer\u3002\u90a3\u4e48\u8fd9\u56db\u79cd\u65b9\u5f0f\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n<p>\u5047\u8bbe\u6709\u4ee5\u4e0b\u4e24\u4e2adataframe\uff1aA\u3001B\u3002A\u6709\u5c5e\u6027user_id\u3001sku_id\uff0cB\u6709\u5c5e\u6027sku_id\u3001cate\u3002\u5c1d\u8bd5\u4f7f\u75284\u79cd\u65b9\u5f0f\u6309\u7167sku_id\u8fde\u8868\u64cd\u4f5c\u3002<\/p>\n<table style=\"border-collapse: collapse; width: 378pt;\" border=\"0\" width=\"504\" cellspacing=\"0\" cellpadding=\"0\">\n<colgroup>\n<col style=\"width: 54pt;\" span=\"7\" width=\"72\" \/> <\/colgroup>\n<tbody>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">A<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">user_id<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">sku_id<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">B<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">sku_id<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">label<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">0<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">0<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">101<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">0<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">101<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">1<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">0<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">109<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">102<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">2<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">102<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">2<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">103<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">2<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">3<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">100<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">3<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">104<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">3<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">4<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">1<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">99<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">4<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">105<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">3<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">5<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">2<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">111<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">5<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">106<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">4<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">6<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">3<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">112<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">6<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">107<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">5<\/td>\n<\/tr>\n<tr style=\"height: 13.5pt;\">\n<td class=\"xl65\" style=\"height: 13.5pt; width: 54pt;\" width=\"72\" height=\"18\">7<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">4<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">108<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\"><\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">7<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">108<\/td>\n<td class=\"xl65\" style=\"width: 54pt;\" width=\"72\">5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>1.inner\uff1adf1\u548cdf2\u7684\u5de6\u53f3\u4f4d\u7f6e\u65e0\u5f71\u54cd\uff0c\u53d6df1\u548cdf2\u5171\u6709\u7684sku_id\uff0c\u5e76\u6dfb\u52a0df1\u7684\u5c5e\u6027user_id\u548cdf2\u7684\u5c5e\u6027label\uff0c\u4e0b\u8868\uff1adf1 inner join df2<\/p>\n<table width=\"268\">\n<tbody>\n<tr>\n<td width=\"72\">(A|B)<\/td>\n<td width=\"52\">sku_id<\/td>\n<td width=\"72\">label<\/td>\n<td width=\"72\">user_id<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>101<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>102<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>108<\/td>\n<td>5<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.left:df1\u548cdf2\u7684\u5de6\u53f3\u4f4d\u7f6e\u6709\u5f71\u54cd\uff0c\u5982df1\u5de6\u8fde\u63a5df2\uff0c\u5219\u53d6df1\u7684\u6240\u6709\u884c\uff0c\u5982\u679c\u8be5\u884csku_id\u7684df2\u53ef\u67e5\u5230\uff0c\u5219\u6dfb\u52a0df2\u7684label\uff0c\u5426\u5219\u6dfb\u52a0\u7a7a\u503c\uff0c\u4e0b\u8868\uff1adf1 left join df2<\/p>\n<table width=\"268\">\n<tbody>\n<tr>\n<td width=\"72\">(A|B)<\/td>\n<td width=\"52\">sku_id<\/td>\n<td width=\"72\">user_id<\/td>\n<td width=\"72\">label<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>109<\/td>\n<td>0<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>102<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>100<\/td>\n<td>1<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>99<\/td>\n<td>1<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>111<\/td>\n<td>2<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>112<\/td>\n<td>3<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>108<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3.right\uff1adf1\u548cdf2\u7684\u5de6\u53f3\u4f4d\u7f6e\u6709\u5f71\u54cd\uff0c\u540c\u5de6\u8fde\u63a5\uff0cdf1 left join df2==df2 right join df1<\/p>\n<p>4.outer:df1\u548cdf2\u7684\u5de6\u53f3\u4f4d\u7f6e\u65e0\u5f71\u54cd\uff0c\u5982df1\u7684sku_id\u4e0edf2\u5339\u914d\uff0c\u5219\u5408\u5e76df1\u7684\u884c\u4e0edf2\u7684\u884c\uff0c\u5426\u5219\u586b\u5145\u7a7a\u503c<\/p>\n<table width=\"268\">\n<tbody>\n<tr>\n<td width=\"72\">(A|B)<\/td>\n<td width=\"52\">sku_id<\/td>\n<td width=\"72\">user_id<\/td>\n<td width=\"72\">label<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>101<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>109<\/td>\n<td>0<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>102<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>100<\/td>\n<td>1<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>99<\/td>\n<td>1<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>111<\/td>\n<td>2<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>112<\/td>\n<td>3<\/td>\n<td>NaN<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>108<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>103<\/td>\n<td>NaN<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>104<\/td>\n<td>NaN<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>105<\/td>\n<td>NaN<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td>106<\/td>\n<td>NaN<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td>107<\/td>\n<td>NaN<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>1,314 Views\u8fde\u8868\u67e5\u8be2\u65b9\u5f0f\u5171\u5305\u62ec4\u79cd\uff1ainner\u3001left\u3001right\u3001outer\u3002\u90a3\u4e48\u8fd9\u56db\u79cd\u65b9\u5f0f\u6709\u4ec0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-st"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=605"}],"version-history":[{"count":2,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":1211,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions\/1211"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}