0

I have two dataframes df and df1. I want to join the both dataframes and get the output in different ways

df
City    Date        Wind    Temperature
London  5/11/2019   14        5
London  6/11/2019   28        6
London  7/11/2019   10        5
Berlin  5/11/2019   23        12
Berlin  6/11/2019   24        12
Berlin  7/11/2019   16        16
Munich  5/11/2019   12        10
Munich  6/11/2019   33        11
Munich  7/11/2019   44        13
Paris   5/11/2019   27        6
Paris   6/11/2019   16        7
Paris   7/11/2019   14        8
Paris   8/11/2019   10        6

df1
ID     City   Delivery_Date Provider
1456223 London  7/11/2019   Amazon
1456345 London  6/11/2019   Amazon
2345623 Paris   8/11/2019   Walmart
1287456 Paris   7/11/2019   Amazon
4568971 Munich  7/11/2019   Amazon
3456789 Berlin  6/11/2019   Walmart

Output1

ID  City    Delivery_Date   Wind    Temperature
1456223 London  7/11/2019   10        5
1456345 London  6/11/2019   28        6
2345623 Paris   8/11/2019   10        6
1287456 Paris   7/11/2019   14        8
4568971 Munich  7/11/2019   44       13

Output 2

Here the weather details of the Item should displayed till its delivery date is met

ID  City    Delivery_Date   Wind    Temperature
1456223 London  5/11/2019   14  5
1456223 London  6/11/2019   28  6
1456223 London  7/11/2019   10  5
1287456 Paris   5/11/2019   27  6
1287456 Paris   6/11/2019   16  7
1287456 Paris   7/11/2019   14  8

How can this be done.

aeapen
  • 871
  • 1
  • 14
  • 28
  • 1
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – pissall Nov 05 '19 at 04:56
  • could you elaborate logic for output2? – kiran gadhe Nov 05 '19 at 05:32
  • @kirangadhe, I am trying to extract all the avialable weather feeds from df for the particular shipment id for that location which less than to the delivery date.For eg.`1287456 is having planned delivery date of 7/11/2018.I so want to display the weather details for 5,6,7 november for that record. – aeapen Nov 05 '19 at 06:16
  • @ashokeapen kindly look below if it woks for you – kiran gadhe Nov 05 '19 at 06:26
  • @kirangadhe, I want also the grouping to be done based id . For eg .`2345623 you will have 4 records showing weather from 5 to 8 November'` and `1287456 you will have 3 records from 5 to7 nov – aeapen Nov 05 '19 at 06:44

2 Answers2

1

considering DF and DF1 as data frames as you explained.

import pandas as pd 
output1 = pd.merge(DF1, DF,left_on = ['City','Date'] ,right_on = ['City','Delivery_Date'], how='inner' ) 
res1 = df1.groupby('City').max() [['Delivery_Date']]
result1 = pd.merge(df,res1, on ='City')
output2 = result1 [result1['Date'] <= result1['Delivery_Date']]

enter image description here

kiran gadhe
  • 733
  • 3
  • 11
0

You can use df.merge

import pandas as pd
df.merge(df1[['City','Delivery_Date','ID']],left_on = ['City','Date'] ,right_on = ['City','Delivery_Date'],how='inner')
Khakhar Shyam
  • 459
  • 2
  • 11