1

I'm trying to import multiple datasets into a single data frame through a function.

# function to import each of the new datasets 
def csvImport(yearOfDataset):
import glob, os
for items in yearOfDataset:
    # dataset name 
    ds = pd.concat(map(pd.read_csv, glob.glob(os.path.join("PSNI_StreetCrime_"+str(yearOfDataset)),"*.csv")))

I want to pass the argument to the function as follows, as it means I can call it quicker for the multiple folders I have; The folder name follow the pattern ChildFolder_YYYY

csvImport('2014')

When running the above, these are the errors being returned.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-61-bba2086ac576> in <module>()
----> 1 csvImport('2014')

<ipython-input-56-0459a8272784> in csvImport(yearOfDataset)
  2 def csvImport(yearOfDataset):
  3     import glob, os
----> 4     sd = pd.concat(map(pd.read_csv, glob.glob(os.path.join("Datasets/PSNI_StreetCrime_"+yearOfDataset),"*.csv")))

TypeError: glob() takes 1 positional argument but 2 were given

I'm new to Pandas, and semi-new to Python so help would be greatly appreciated, the various changes I've tried have been unsuccessful.

  • As stated in the error, you're passing 2 arguments into the `glob.glob` function, your `os.path.join()` and `"*.csv"` and it only takes one. Did you mean to put the `.csv` inside the other function? – G. Anderson Oct 16 '18 at 18:30
  • `sd = pd.concat(map(pd.read_csv, glob.glob(os.path.join("Datasets/PSNI_StreetCrime_"+yearOfDataset +"/*.csv")))` – Kenan Oct 16 '18 at 19:01

0 Answers0