Python Forum

Full Version: Concatenate two files with different columns into one dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have a question that it is possible easy to answer. I am importing a csv file into a dataframe but rows have different number of columns. I solved this adding names=range(100) and it worked fine:

datos=pd.read_csv('C:/Personal/datos.csv', delimiter=',', header=None, names=range(100))


Now I am trying to import multiple files and concatenate into one dataframe but I am having issues. This is the code I am currently using:
import pandas as pd
import glob

df = pd.concat(map(pd.read_csv, glob.glob('C:/Personal/test/*.csv')))
This small code is working well when rows have the same columns but I am having the same issue than before where rows have a different amount of columns and getting the following error:
Error:
pandas.errors.ParserError: Error tokenizing data. C error: Expected 54 fields in line 3, saw 60
I suppose that I should add names=range(100) as before but not sure where. Thank you.
Ok I have been able to solve this with this code:

files = glob('C:/Personal/data*.log')
dataframe = pd.concat([pd.read_csv(f, names=range(100)) for f in files])

Thnaks anyway :-)