Gloria Dalla Costa
Gloria Dalla Costa

Reputation: 397

How to merge two datasets with pd concat

I have two datasets like thes down, and for both the column ID_NIL is the index:

result3.head()
restaurants_count   Population  Pricexm2    Pricexm2a
ID_NIL              
1   205 16765   4950    36.0
2   114 18492   6250    38.0
4   55  15364   5050    33.0
5   44  13686   3300    24.0
6   81  20008   4000    25.0

result3.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 79 entries, 1 to 88
Data columns (total 4 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   restaurants_count  79 non-null     int64  
 1   Population         79 non-null     int64  
 2   Pricexm2           79 non-null     int64  
 3   Pricexm2a          36 non-null     float64
dtypes: float64(1), int64(3)
memory usage: 5.6 KB


restloc.head()
    restaurant_id   lat lng NIL
ID_NIL              
2   0   45.47940    9.18209 BRERA
2   7   45.47149    9.18479 BRERA
2   16  45.47585    9.18945 BRERA
2   35  45.46948    9.18211 BRERA
2   50  45.47789    9.18899 BRERA

restloc.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2265 entries, 2 to 74
Data columns (total 4 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   restaurant_id  2265 non-null   int64  
 1   lat            2265 non-null   float64
 2   lng            2265 non-null   float64
 3   NIL            2265 non-null   object 
dtypes: float64(2), int64(1), object(1)
memory usage: 168.5+ KB

Basically, I have 2265 restaurants in restloc, each with its area of the city where it is located (ID_NIL). In the result3 dataset I have specific info for each area of the city, and I would like to have a single dataset, with all the restaurants in rows, and in columns the area that restaurant belong to and all the info related to that area. How can i merge them? i tried this but failed:

result4=pd.concat([restloc, result3], axis=1)

ValueError: Shape of passed values is (181279, 8), indices imply (2265, 8) 

Upvotes: 1

Views: 71

Answers (1)

Riccardo Bucco
Riccardo Bucco

Reputation: 15384

Here is a possible solution:

result4 = pd.merge(restloc, result3, how='left', on='ID_NIL')

Since you're doing a left join, you might have some restaurants that end up having empty locations info (if your database does not have it). If you want to keep only the restaurants that have corresponding info about the location then just do

result4 = pd.merge(restloc, result3, on='ID_NIL')

Upvotes: 2

Related Questions