Merge two CSV files horizontally by appending columns side by side
Paste Side by Side
Paste Side by Side combines two CSV files horizontally by placing the columns from the second file directly beside the columns from the first file.
Rows are matched strictly by position. The first row of File 1 aligns with the first row of File 2, the second row aligns with the second row, and so on.
File 1:
Gene,Expression
BRCA1,12
TP53,25
File 2:
Sample,Condition
S1,Control
S2,Treated
Output:
Gene,Expression,Sample,Condition
BRCA1,12,S1,Control
TP53,25,S2,Treated
paste -d',' file1.csv file2.csv > combined.csv
import pandas as pd
df1 = pd.read_csv("file1.csv")
df2 = pd.read_csv("file2.csv")
combined = pd.concat(
[df1, df2],
axis=1
)
combined.to_csv(
"combined.csv",
index=False
)