pandas :水平组合两个数据框 [英] Pandas: Combining Two DataFrames Horizontally
本文介绍了 pandas :水平组合两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个Pandas DataFrame,每个都有不同的列.我基本上希望将它们水平粘合在一起(它们每个都具有相同的行数,所以这不应该成为问题).
I have two Pandas DataFrames, each with different columns. I want to basically glue them together horizontally (they each have the same number of rows so this shouldn't be an issue).
必须有一种简单的方法来完成此操作,但是我已经阅读了文档,而concat
不是我想要的(我不认为).
There must be a simple way of doing this but I've gone through the docs and concat
isn't what I'm looking for (I don't think).
有什么想法吗?
谢谢!
推荐答案
concat
确实是您想要的,您只需要为它传递一个与默认值不同的轴"参数值即可.下面的代码示例:
concat
is indeed what you're looking for, you just have to pass it a different value for the "axis" argument than the default. Code sample below:
import pandas as pd
df1 = pd.DataFrame({
'A': [1,2,3,4,5],
'B': [1,2,3,4,5]
})
df2 = pd.DataFrame({
'C': [1,2,3,4,5],
'D': [1,2,3,4,5]
})
df_concat = pd.concat([df1, df2], axis=1)
print(df_concat)
这篇关于 pandas :水平组合两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文