I want to combine csv as header

Asked 2 years ago, Updated 2 years ago, 86 views

I want to combine csvs with the same number of columns so that one side is set to header

What I've tried

  • header.csv
Columns: [a,b,c]Index: [ ]
  • axis.csv
Columns: [1,2,3]
     [4,5,6]
df=pd.read_csv('header.csv')
df1 = pd.read_csv('axis.csv')
df2 = pd.concat ([df1, df2])

Ideal

 a,b,c
1,2,3
4,5,6

Reality

 1, 2, 3, a, b, c
4,5,6

python pandas csv

2022-09-30 14:41

1 Answers

If one side is only a header, you can use columns instead.

import pandas as pd

df1 = pd.read_csv('header.csv', header=0)
df2 = pd.read_csv('axis.csv', header = None)
df2.columns = df1.columns
print(df2)

Output:

abc
0  1  2  3
1  4  5  6


2022-09-30 14:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.