{"id":3369,"date":"2020-07-30T22:15:24","date_gmt":"2020-07-30T20:15:24","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3369"},"modified":"2020-07-30T22:15:24","modified_gmt":"2020-07-30T20:15:24","slug":"joining-in-pandas","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3369","title":{"rendered":"Joining in Pandas"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python has Pandas its tool to handle matrices, also known as dataframes in Pyhon speak. It is possible to join dataframes, much like joining tables. I found a nice example on the internet with several possible choices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a first step, two dataframes were declared:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first dataframe:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">customer=pd.DataFrame({<br>'id':[1,2,3,4,5,6,7,8,9],<br>'name':['Olivia','Aditya','Cory','Isabell','Dominic','Tyler','Samuel','Daniel','Jeremy'],<br>'age':[20,25,15,10,30,65,35,18,23],<br>'Product_ID':[101,0,106,0,103,104,0,0,107],<br>'Purchased_Product':['Watch','NA','Oil','NA','Shoes','Smartphone','NA','NA','Laptop'],<br>'City':['Mumbai','Delhi','Bangalore','Chennai','Chennai','Delhi','Kolkata','Delhi','Mumbai']<br>})<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second dataframe:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">product=pd.DataFrame({<br>'Product_ID':[101,102,103,104,105,106,107],<br>'Product_name':['Watch','Bag','Shoes','Smartphone','Books','Oil','Laptop'],<br>'Category':['Fashion','Fashion','Fashion','Electronics','Study','Grocery','Electronics'],<br>'Price':[299.0,1350.50,2999.0,14999.0,145.0,110.0,79999.0],<br>'Seller_City':['Delhi','Mumbai','Chennai','Kolkata','Delhi','Chennai','Bengalore']<br>})<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The inner join:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ff_inner = pd.merge(product,customer,on= 'Product_ID')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An outer join<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ff_outer = pd.merge(product,customer,left_on='Product_ID',right_on='Product_ID',how='outer',indicator=True)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The left join is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ff_left = pd.merge(product,customer,left_on=['Product_ID','Seller_City'],right_on=['Product_ID','City'],how='left')<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python has Pandas its tool to handle matrices, also known as dataframes in Pyhon speak. It is possible to join dataframes, much like joining tables. I found a nice example on the internet with several possible choices. As a first step, two dataframes were declared: import pandas as pd The first dataframe: customer=pd.DataFrame({&#8216;id&#8217;:[1,2,3,4,5,6,7,8,9],&#8217;name&#8217;:[&#8216;Olivia&#8217;,&#8217;Aditya&#8217;,&#8217;Cory&#8217;,&#8217;Isabell&#8217;,&#8217;Dominic&#8217;,&#8217;Tyler&#8217;,&#8217;Samuel&#8217;,&#8217;Daniel&#8217;,&#8217;Jeremy&#8217;],&#8217;age&#8217;:[20,25,15,10,30,65,35,18,23],&#8217;Product_ID&#8217;:[101,0,106,0,103,104,0,0,107],&#8217;Purchased_Product&#8217;:[&#8216;Watch&#8217;,&#8217;NA&#8217;,&#8217;Oil&#8217;,&#8217;NA&#8217;,&#8217;Shoes&#8217;,&#8217;Smartphone&#8217;,&#8217;NA&#8217;,&#8217;NA&#8217;,&#8217;Laptop&#8217;],&#8217;City&#8217;:[&#8216;Mumbai&#8217;,&#8217;Delhi&#8217;,&#8217;Bangalore&#8217;,&#8217;Chennai&#8217;,&#8217;Chennai&#8217;,&#8217;Delhi&#8217;,&#8217;Kolkata&#8217;,&#8217;Delhi&#8217;,&#8217;Mumbai&#8217;]}) The second [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3369","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3369"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3369\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3369"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}