0

Iam trying to concatenate multiple csv but the output file has all the data in a single row. How to add a new line character while giving a output. Here is a sample code of mine

from os import chdir
from glob import glob
import pandas as pdlib

# Produce a single CSV after combining all files
def produceOneCSV(list_of_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files], sort=False)
   # Convert the above object into a csv file and export

   result_obj.to_csv(file_out, index=False, encoding="utf-8")

# Move to the path that holds our CSV files
csv_file_path = 'Path_for_all_the_csv/'
chdir(csv_file_path)

# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob('*.csv')]
print(list_of_files)

file_out = "ConsolidateOutput.csv"
produceOneCSV(list_of_files, file_out)


1 Answer 1

2

When you concatenate multiple dataframes you can specify the axis, whether if you prefer to concatenate over columns or rows.

For example

   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files], sort=False, axis='columns')

Take a look at the concat function documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried with the suggestion and follow the document to make "axis=1" as well, but still getting the same result. Is it possible to add a new line character at the end of each row?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.