-1

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Checkout: [How to merge two dataframes side-by-side?](https://stackoverflow.com/questions/23891575/how-to-merge-two-dataframes-side-by-side) – DarrylG Nov 30 '22 at 14:30
  • 1
    Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Nov 30 '22 at 14:44
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Nov 30 '22 at 16:26

1 Answers1

1

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)
Vini
  • 875
  • 1
  • 7
  • 14