How to merge more than one dataframes with each other?

Viewed 43

All these three dataframes have same number of rows. While i do merge with concat function original dataframe get's deleted and it's only have car1 and car2 whenever i am adding car1,car2 to car dataframe all the contents get deleted and replace with car1,car2

car1=pd.get_dummies(car.company)
car2=pd.get_dummies(car.fuel_type)
car=pd.concat([car1, car2], axis=1)
print(car2.shape)
print(car1.shape)
print(car.shape) # original dataframe
o/p 
  (815, 3)
  (815, 25)
  (815, 6)

   name                                   company   year  Price  kms_driven fuel_type
  0 Hyundai Santro Xing XO eRLX Euro III  Hyundai   2007  80,000    45,000 kms  Petrol
2 Answers

add car dataframe with your list of dataframes you concat:

car = pd.concat([car, car1, car2], axis=1)

This is in the case you wish you add car1, car2 to car. Otherwise, I probably misunderstood you. Please also review our community guidelines on how to ask proper questions. For example, your question lack examples (how the dataframes look like) and what the output should be. It will be much easier to answer your question if you follow the guidelines.

If I understand correctly, you would like the data from the original car DataFrame to be retained in your final DataFrame, using the same variable name car.

To do that, you would just need to include car in the concat function in the list of DataFrames:

car = pd.concat([car, car1, car2], axis=1) 

If you don't want to include the original columns with the text values that you have encoded (car.company and car.fuel_type), then:

car = pd.concat([car.drop(["company", "fuel"], axis=1), car1, car2], axis=1). 
Related