简体   繁体   English

如何将多个数据框相互合并?

[英]How to merge more than one dataframes with each other?

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虽然我确实与 concat function 原始 dataframe 合并,但它被删除了,它只有 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)

add car dataframe with your list of dataframes you concat:添加car dataframe 与您连接的数据框列表:

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

This is in the case you wish you add car1, car2 to car.在这种情况下,您希望将 car1、car2 添加到 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.例如,您的问题缺少示例(数据框的外观)以及 output 应该是什么。 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.如果我理解正确,您希望将原车 DataFrame 中的数据保留在您的最终 DataFrame 中,使用相同的变量名 car。

To do that, you would just need to include car in the concat function in the list of DataFrames:为此,您只需将 car 包含在数据帧列表中的 concat function 中:

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.company 和 car.fuel_type)中包含原始列,则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM