Consolidate . csv files with Python

Asked

Viewed 193 times

1

Good Night, please help me with the question below?

I am developing a program that reads all csv’s with the same content of a folder and consoles them in a single file, disregarding the first line, but when executing the code below the final file has no data one under the other and rather next to each other, how can I correct?

Final file: Date,Name,Value,Date,Name,Value,Date,Name,Value,Date,Name,Value,Date,Name,Value,Date,Name,Value,Date,Date,Name,Value, 01/01/2020,Test,1,01/01/2020,Test,1,01/01/2020,Test,1,01/01/2020,Test,1,01/01/2020,Test,1,01/01/2020,Test,1,01/01/2020,Test,1

    import pandas as pd
    import numpy as np
    
    diretorio = "C:/Users...{}"
    consolidado = []
    
    for i in lista_arquivos:
        consolidado.append(pd.read_csv(diretorio.format(i), sep=';'))

arq_consolidado = pd.concat(consolidado, axis=1, join='inner').sort_index()
arq_consolidado.to_csv(r'C:\Users...arq_consolidado.csv', index = False)

1 answer

1


It’s because you set axis=1 in function pd.concat(), when it should be axis=0, axis='index', or without specifying the argument, since this is the default. Change this and solve the problem :)

And just out of curiosity, you can create the list of files . csv by understanding too:

consolidado = [pd.read_csv(diretorio.format(arquivo)) for arquivo in lista_arquivos]
arq_consolidado = pd.concat(consolidado, join='inner')
  • Thank you Cainan I used this compression and it worked super well!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.