2

This problem can be solved by using different variable names in the loop. However, I need to check whether we can find the length of elements of List which can store data frames.

I have written code to read all .csv file stored in a folder. I have created the list containing the characters. Now, I am reading one by one csv files from a folder and storing as data frames in elements of list. Hence, all Elements of list contain data frame which is obtained by reading csv files. I.e., List element 0 - "a" contain one data frame, List element 1 - "b" contain other data frames and so on. I need to find the length of Data frames contained by elements of the list. I am using len(), but this reflects the only length of the list. Kindly suggest me some solution.

Below mentioned is the code.

url = "C:/Users/SONY/Desktop/SEdge/Proec/Day/Master Files/20-12-2017/"

count = 0;

lst = ["a", "b", "c", "d"]

os.chdir(url)

for file in glob.glob("*.csv"):

    join = url + file;

    lst[count] = pd.read_csv(join)

    count +=1;

print(len(lst[1]))

soni smit
  • 43
  • 2
  • 8
  • 1
    Possible duplicate of [How do you create different variable names while in a loop? (Python)](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop-python) – Bharath M Shetty Dec 22 '17 at 06:27
  • 1
    You are not suppose to store data in a string, they are called variables. Though there is a dtype called dictionary to do exactly what you want. A bit more understanding of data structrures is required from your side. – Bharath M Shetty Dec 22 '17 at 06:29
  • I have no clue why you're filling a list with a, b, c, d and then subsequently replacing it with something else. Why not just create an empty list and then use `list.append`? – cs95 Dec 22 '17 at 06:32
  • @cᴏʟᴅsᴘᴇᴇᴅ perhaps a misunderstanding of dictionary over a list. – Bharath M Shetty Dec 22 '17 at 06:33
  • @Dark I'm not so sure they even want a dictionary here because they're just inserting by index. It seems like they just want to put everything into a list and measure the length of each one. But this code really makes it hard to understand what they really want to do. Perhaps some input and expected output would help. – cs95 Dec 22 '17 at 06:34
  • @COLDSPEED... I want to create different variables. For this, I was not aware of the dictionary, hence I use list element to store different data frames. – soni smit Dec 22 '17 at 06:38
  • @Dark, in my code, elements of lists contain data frames, but is it possible to find the length of data frames?? – soni smit Dec 22 '17 at 06:38
  • 1
    @sonismit Why not find the length as you read it? `df = pd.read_csv(join); print(len(df)); di[lst[count]] = df` – cs95 Dec 22 '17 at 06:39

1 Answers1

2

You cant store data in a string. You have to store them in a variable. Since you are looping over a bunch of csv file the opt data structure is dictionary. i.e

An improvement of your own code :

lst = ['a','b','c','d']
di = {} 

for count, file in enumerate(glob.glob("*.csv")):
    join = url + file;
    di[lst[count]] = pd.read_csv(join)

di['a'] stores the first csv file. Similarly di['b] the second one and so on.

Now if you do len(di[lst[1]]) this will give you the length of the csv file 1.

For more details about datastructures I suggest you to go through the documentation

Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108