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.
JavaScript
x
10
10
1
if 'myName' in user or user == '/Users/rick':
2
file1 = pd.read_csv(path + '/Data Exports/file1.csv')
3
file2 = pd.read_csv(path + '/Data Exports/file2.csv')
4
file3 = pd.read_csv(path + '/Data Exports/file3.csv')
5
file4 = pd.read_csv(path + '/Data Exports/file4.csv')
6
file5 = pd.read_csv(path + '/Data Exports/file5.csv')
7
file6 = pd.read_csv(path + '/Data Exports/file6.csv')
8
file7 = pd.read_csv(path + '/Data Exports/file7.csv')
9
file8 = pd.read_csv(path + '/Data Exports/file8.csv')
10
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
JavaScript
1
24
24
1
import glob
2
3
# list
4
datas = [
5
pd.read_csv(f'{path}/Data Exports/file{i}.csv')
6
for i in range(1, 9)
7
if ('myName' in user or user == '/Users/rick')
8
]
9
10
# list whatever csv file name, will read all csv files
11
datas = [
12
pd.read_csv(filepath)
13
for filepath in glob.glob(f'{path}/Data Exports/*.csv')
14
if ('myName' in user or user == '/Users/rick')
15
]
16
17
18
# map
19
datas = {
20
"file%s" % i: pd.read_csv(f'{path}/Data Exports/file{i}.csv')
21
for i in range(1, 9)
22
if ('myName' in user or user == '/Users/rick')
23
}
24