Bowen Liu
Bowen Liu

Reputation: 1127

How to create a list with just dataframe names as elements to be used as input for other function call

I'm trying to concatenate multiple csv files with similar file names. The way I want to do it is to write a function and run the function using a for loop.

The function I wrote:

def GetEach(LastBit):
    FilePath = r'file:///Path\FLAG_' + LastBit + '.csv'
    df = pd.read_csv(FilePath)
    df = df[['CRN', 'ALCOHOL_RELATED', 'CELL_PHONE', 'DRINKING_DRIVER', 'DRUG_RELATED',
       'ILLEGAL_DRUG_RELATED', 'DRUGGED_DRIVER',
       'IMPAIRED_DRIVER' ]]
    return df 

And then the for loop I wrote is shown below. The reason for the code is that the filenames of the csv files are almost identical, except for different year.

for i in range(2, 8):
    MyInput = '201' + str(i) + '_Northampton'
    r.append(GetEach(MyInput)) 

What I want to finally acquire is something like WhatIWant = [GetEach(2012), GetEach(2013)...] and then I can just run pd.concat(WhatIWant ) to get what I want. But apparently my code will make the dataframe totally unravel in the list r.

How shall I fix my code? Or is there a much better way of doing this?

Thanks.

Upvotes: 0

Views: 59

Answers (1)

Niels Henkens
Niels Henkens

Reputation: 2696

This is a good way to read the csv's, append the dataframes to a list and then concat them into 1 big dataframe.

Your code sample is not really complete. Does your code look something like this?

import pandas as pd

def GetEach(LastBit):
    FilePath = r'file:///Path\FLAG_' + LastBit + '.csv'
    df = pd.read_csv(FilePath)
    df = df[['CRN', 'ALCOHOL_RELATED', 'CELL_PHONE', 'DRINKING_DRIVER', 'DRUG_RELATED', 'ILLEGAL_DRUG_RELATED', 'DRUGGED_DRIVER','IMPAIRED_DRIVER' ]]
    return df 

r = [] # Start with an empty list!

for i in range(2, 8):
    MyInput = '201' + str(i) + '_Northampton'
    r.append(GetEach(MyInput)) 

df_complete = pd.concat(r)

If so, have you checked that the individual df's are correct?

Upvotes: 1

Related Questions