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
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,317 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  Concatenate array for 3D plotting armanditod 1 1,372 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 14,155 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Several xml files to dataframe mfernandes 2 1,749 Sep-20-2022, 07:58 PM
Last Post: mfernandes
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 2,955 Aug-19-2022, 11:07 AM
Last Post: dm222
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 3,742 Apr-27-2022, 12:44 PM
Last Post: hasiro
  How to concatenate filepath with variable? Mark17 2 9,544 Jan-31-2022, 09:13 PM
Last Post: Mark17
  Apply fillna to multiple columns in dataframe rraillon 2 3,926 Aug-05-2021, 01:11 PM
Last Post: rraillon
  How to rename dataframe columns based on the content in an index? ar_mahdavi 2 3,290 Jun-07-2021, 06:09 AM
Last Post: ricslato
  Concatenate str JohnnyCoffee 2 3,744 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee

Forum Jump:

User Panel Messages

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