Skip to content
Advertisement

How to create a 2-dimensional list with dataframes in Python

I have the following structure in my file system. enter image description here

So I have a folder with the name “Data” and there I have 3 subfolders “Building1”, “Building2” and “Building3”. Each of those building folders contain 3 subfolders with “Day1”, “Day2” and “Day3”. In each of those day folders there is a csv-file with the name “values.csv”. Now I would like to read that into a 2-dimensional list and I would like to know, if and how this is possible.

I tried the following:

import pandas as pd
list_df = [pd.read_csv(f"C:/Users/User1/Desktop/Data/Building{indexBuilding }/Day{indexDay }/values.csv", sep =";")  for indexBuilding in range (1,4) for indexDay in range (1,4)]

This reads all the files but just creates a 1-dimensional list. I would like to have a 2-dimensional list such that I can access the single dataframes by using list_df [indexBuilding ] [indexDay]

Do you know how I can do that?

Advertisement

Answer

IUUC, you can try

list_df = [[pd.read_csv(f"C:/Users/User1/Desktop/Data/Building{indexBuilding }/Day{indexDay }/values.csv", sep =";") for indexDay in range(1,4)] for indexBuilding in range(1,4)]

Idea is to use a inner for loop

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement