You can use itertools.starmap.
This function takes:
- a function as the first argument,
- and runs it on each set of parameters from an iterable (second
argument - a list of tuples).
I ran the following example:
import itertools as it
# read_csv wrapper
def rd_csv(fn, separ, hdr):
print(f'File: {fn} / sep: {separ} / header: {hdr}')
if hdr is None:
return pd.read_csv(fn, sep=separ,
names=['ind', 'POLL_X', 'POLL_Y', 'POLL_Z', 'POLL_DNW', 'AVal', 'ZVal'])
else:
return pd.read_csv(fn, sep=separ, header=hdr)
# Parameters for consecutive calls (filename, separator, header)
inputs = [ ('Input_1.csv', ',', 0), ('Input_2.csv', ';', None) ]
# Read all files
res = pd.concat(it.starmap(rd_csv, inputs), ignore_index=True)
Test printouts during exacutions are:
File: Input_1.csv / sep: , / header: 0
File: Input_2.csv / sep: ; / header: None
(drop them in the target version).
Note a "special treatment" of the case when header == None.
The reason is that:
- DataFrames read with header=0 will have column names read from the
first line.
- DataFrames read with header=None will have column names set to
consecufive numbers,
so on concatenation these columns will not be concatenated properly.
To take it into accouunt, when header == None, the wrapper function
must specify column names (names parameter) on its own (so it must include
hard-coded column names).
The reationale behind this combination is that if all these source files
are to be concatenated into one DataFrame, they should have a common set
of column names, even if some input files do not actually contain them
in the first row.