Black
Black

Reputation: 4644

Python: Multiple CSVs

I am trying to read a number of CSV files into arrays like

for files in folder:
    with open(files) as f:
         df = pd.read_csv(f)
         result = function(df)
         print result

The problem is this only reads a file at a time not multiple files. My aim is to pass each of these dataframes through a function and create an output for each dataframe. The function is done, all I need to do is read these files into separate dataframes. Is there a method to doing this?

Upvotes: 0

Views: 92

Answers (1)

furas
furas

Reputation: 142651

Using list for dataframes

df = []

for one_file in folder:
        with open(one_file) as f:
            df.append( pd.read_csv(f) )

print df[0]
print df[1] 
# etc.

or

df = []
result = []

for one_file in folder:
        with open(one_file) as f:
            df.append( pd.read_csv(f) )
            result.append( function(df[-1]) )
            print result[-1]

print df[0]
print df[1] 
# etc.
print df[-1] # last df

print result[0]
print result[1] 
# etc.
print result[-1] # last result

Upvotes: 1

Related Questions