1

I have tried this:

>>> df = [pd.read_csv(x,header=None,names=["L1","L2","cache","cached","result"]) for x in iglob(os.path.join("test","**","*.csv"), recursive=True)]
>>> df
[   L1  L2  cache  cached  result
0   0   0      0       0       0
1   1   2      3       4       5
2   1   1      1       1       1
3   2   2      2       2       2
4   4   4      4       4       4,    L1  L2  cache  cached  result
0   1   2      3       4       5
1   1   2      3       4       5
2   3   4      5       6       7
3   2   1      3       2       4]

The folder structure is:

test
|
|_______ wait
         |
         |______ 0.2322.csv
         |______ 1.234.csv

The two files contain:
0.2322.csv

0,0,0,0,0
1,2,3,4,5
1,1,1,1,1
2,2,2,2,2
4,4,4,4,4

1.234.csv

1,2,3,4,5
1,2,3,4,5
3,4,5,6,7
2,1,3,2,4

When I am trying to access the dataframes from the df array, I have to call it with the index values as 0,1 i.e. df[0] and df[1].

But I want to call the dataframe of the respective files with there file name as index as df["0.2322"] and df["1.234"]. But I am not getting a clue how that is possible. Please let me know what I can do to achieve what I expect.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

1 Answers1

1

I think you need dictionary comprehension with parse filename without extension:

import os

#https://stackoverflow.com/a/678242
df = {os.path.splitext(x)[0]: pd.read_csv(x,header=None,names=["L1","L2","cache","cached","result"]) for x in iglob(os.path.join("test","**","*.csv"), recursive=True)}

EDIT:

#https://stackoverflow.com/a/37760212
df = {os.path.splitext(os.path.basename(x))[0]: pd.read_csv(x,header=None,names=["L1","L2","cache","cached","result"]) for x in iglob(os.path.join("test","**","*.csv"), recursive=True)}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252