Python Forum
Concatenate two files with different columns into one dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatenate two files with different columns into one dataframe
#1
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.
Reply
#2
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 :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,379 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Several xml files to dataframe mfernandes 2 787 Sep-20-2022, 07:58 PM
Last Post: mfernandes
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,697 Aug-19-2022, 11:07 AM
Last Post: dm222
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,081 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Apply fillna to multiple columns in dataframe rraillon 2 2,417 Aug-05-2021, 01:11 PM
Last Post: rraillon
  How to rename dataframe columns based on the content in an index? ar_mahdavi 2 2,463 Jun-07-2021, 06:09 AM
Last Post: ricslato
  Concatenate str JohnnyCoffee 2 2,918 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  How to sum across variable columns in a dataframe rennerom 2 2,748 Jan-31-2021, 05:44 PM
Last Post: rennerom
  How to split dataframe object rows to columns Mekala 1 2,491 Nov-12-2020, 04:18 PM
Last Post: michael1789
  convert list to five columns dataframe in sequence tonycat 2 2,465 Sep-29-2020, 06:47 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020