alex_bn_lee

导航

[966] Merge two DataFrames horizontally

In Pandas, you can use the pd.concat function to merge two DataFrames horizontally (i.e., along columns). Here's an example:

import pandas as pd

# Sample DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})

# Concatenate along columns (axis=1)
result = pd.concat([df1, df2], axis=1)

print(result)

In this example, pd.concat([df1, df2], axis=1) concatenates df1 and df2 along columns, creating a new DataFrame result with columns from both df1 and df2. The axis=1 parameter specifies the concatenation along columns.

You can adjust column names and indexes based on your specific use case and data.

posted on 2024-02-08 13:56  McDelfino  阅读(1)  评论(0编辑  收藏  举报