Skip to content
Advertisement

Using Functions rather than one liners Python

I am trying to use Python functions to perform data preprocessing. I want to have efficient code. I have like 8 files that I am reading to load them and do some sort of analysis. How would one achieve that using a function and the .head() to read all the CSV files?

For instance instance I have loaded my data from One drive as follows.

 if 'myName' in user or user == '/Users/rick':   
    file1 = pd.read_csv(path + '/Data Exports/file1.csv')
    file2 = pd.read_csv(path + '/Data Exports/file2.csv')
    file3 = pd.read_csv(path + '/Data Exports/file3.csv')
    file4 = pd.read_csv(path + '/Data Exports/file4.csv')
    file5 = pd.read_csv(path + '/Data Exports/file5.csv')
    file6 = pd.read_csv(path + '/Data Exports/file6.csv')
    file7 = pd.read_csv(path + '/Data Exports/file7.csv')
    file8 = pd.read_csv(path + '/Data Exports/file8.csv')

How would I write a .head() to inspect the top 5 rows of all the loaded files?

Advertisement

Answer

list or map comprehension would be better imo

import glob

# list
datas = [
    pd.read_csv(f'{path}/Data Exports/file{i}.csv')
    for i in range(1, 9)
    if ('myName' in user or user == '/Users/rick')
]

# list whatever csv file name, will read all csv files
datas = [
    pd.read_csv(filepath)
    for filepath in glob.glob(f'{path}/Data Exports/*.csv')
    if ('myName' in user or user == '/Users/rick')
]


# map
datas = {
    "file%s" % i: pd.read_csv(f'{path}/Data Exports/file{i}.csv')
    for i in range(1, 9)
    if ('myName' in user or user == '/Users/rick')
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement