0

I am trying to load multiple CSVs into a single pandas dataframe. They are all in one file, and all have the same column structure. I have tried a few different methods from a few different threads, and all return the error 'ValueError: No objects to concatenate.' I'm sure the problem is something dumb like my file path? This is what I've tried:

temps = pd.concat(map(pd.read_csv, glob.glob(os.path.join('./Resources/temps', "*.csv"))))

Also this:

path = r'./Resources/temps'                     
temps_csvs = glob.glob(os.path.join(path, "*.csv"))     

df_for_each_csv = (pd.read_csv(f) for f in temps_csvs)
temps_df = pd.concat(df_for_each_csv, ignore_index = True)```

Thanks for any help!
4
  • you might be right; it's possible pandas is not seeing any file. what do you get for df_for_each_csv? Commented Feb 28, 2021 at 0:21
  • first off make sure when you print temps_csvs that there's actually a list of files in there Commented Feb 28, 2021 at 0:30
  • @sammywemmy <generator object <genexpr> at 0x00000220B47BDC48> Commented Feb 28, 2021 at 0:34
  • @Chris yeah i just fixed my file path and the list populates correctly, but now i get this error: ParserError: Error tokenizing data. C error: Expected 5 fields in line 1394, saw 6 Commented Feb 28, 2021 at 0:35

2 Answers 2

1

It might not be as helpful as other answers, but when I tried running your code, it work perfectly fine. The only difference that conflicted was that I changed the path to be like this:

temps_csvs = glob.glob(os.path.join(os.getcwd(), "*.csv"))

df_for_each_csv = (pd.read_csv(f) for f in temps_csvs)
temps_df = pd.concat(df_for_each_csv, ignore_index = True) 

and put the script in the same folder as to where the csv files are.

EDIT: I saw your comment about how you are having an error ParserError: Error tokenizing data. C error: Expected 5 fields in line 1394, saw 6

This means that the csv files don't have the same number of columns, here is a question that deals with a similar issue, maybe it will help : Reading a CSV file with irregular number of columns using Pandas

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

1 Comment

you're right! there were a few rouge data points chilling in a 6th column. ...i feel silly. thanks for your help!
0

Change tuple to list on the third line. [pd.read_csv(f) for f in temps_csvs]

or add tuple to it: tuple(pd.read_csv(f) for f in temps_csvs)

Tuple Comprehension doesn't work this way. See Why is there no tuple comprehension in Python?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.