Home > Net >  How to combine multiple csv as columns in python?
How to combine multiple csv as columns in python?

Time:12-01

I have 10 .txt (csv) files that I want to merge together in a single csv file to use later in analysis. when I use pd.append, it always merges the files below each other.

I use the following code:

master_df = pd.DataFrame()

for file in os.listdir(os.getcwd()):
    if file.endswith('.txt'):
        files = pd.read_csv(file, sep='\t', skiprows=[1])
        master_df = master_df.append(files)

the output is:

output

what I need is to insert the columns of each file side-by-side, as follows:

The required output

could you please help with this?

CodePudding user response:

To merge DataFrames side by side, you should use pd.concat.

frames = []

for file in os.listdir(os.getcwd()):
    if file.endswith('.txt'):
        files = pd.read_csv(file, sep='\t', skiprows=[1])
        frames.append(files)

# axis = 0 has the same behavior of your original approach
master_df = pd.concat(frames, axis = 1)
  • Related