2015-12-02 4 views
0

J'ai deux dataframes en pandas géants:Comment fusionner deux trames de données avec des valeurs répétées dans Pandas

dilevery_time dispatch_time source_lat source_long Address name 
0 21:39:37.265 21:47:37.265 -73.955741 40.3422  Dmart John 
0 21:39:37.265 21:47:37.265 -73.955741 40.3422  Dmart John 

et l'autre est:

chef_name dish_name dish_price dish_quantity ratings 
0 xyz  Chicken  120   1    4 
1 abc  Paneer  100   2    3 

Je veux rejoindre ces deux dataframes en pandas géants. J'ai effectué une concaténation mais cela ne me permet pas d'effectuer car la première trame de données a des valeurs répétées.

Alors, je l'ai fait:

pd.concat([df1, df2], join='inner', axis=1) 

Mais cela me donne la sortie suivante:

dilevery_time dispatch_time source_long Address name chef_name 
0 21:39:37.265 21:47:37.265 -73.955741 Dmart John xyz 
0 21:39:37.265 21:47:37.265 -73.955741 Dmart John xyz 

    dish_name dish_price dish_quantity ratings 
0 Chicken  120    1    4 
0 Chicken  120    1    4 

Et je veux dans ce format:

dilevery_time dispatch_time source_long Address name chef_name 
0 21:39:37.265 21:47:37.265 -73.955741 Dmart John xyz 
0 21:39:37.265 21:47:37.265 -73.955741 Dmart John abc 

    dish_name dish_price dish_quantity ratings 
0 Chicken  120    1    4 
0 Paneer  100    2    3 

Comment faire chez les pandas?

Répondre

0

C'est parce que dans la première image, vous avez deux fois l'index 0. Vous pouvez utiliser la méthode reset_index, puis obtenir votre résultat:

In [9]: df 
Out[9]: 
    chef_name dish_name dish_price dish_quantity ratings 
0  xyz Chicken   120    1  4 
1  abc Paneer   100    2  3 

In [10]: df1 
Out[10]: 
    chef_name dish_name dish_price dish_quantity ratings 
0  xyz Chicken   120    1  4 
1  abc Paneer   100    2  3 

df1.reset_index(drop=True, inplace 

In [11]: pd.concat([df1, df2], join='inner', axis=1) 
Out[11]: 
    chef_name dish_name dish_price dish_quantity ratings dilevery_time \ 
0  xyz Chicken   120    1  4 21:39:37.265 
1  abc Paneer   100    2  3 21:39:37.265 

    dispatch_time source_lat source_long Address name 
0 21:47:37.265 -73.955741  40.3422 Dmart John 
1 21:47:37.265 -73.955741  40.3422 Dmart John