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]))

8
  • 1
    Possible duplicate of How do you create different variable names while in a loop? (Python) Commented Dec 22, 2017 at 6: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. Commented Dec 22, 2017 at 6: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? Commented Dec 22, 2017 at 6:32
  • @cᴏʟᴅsᴘᴇᴇᴅ perhaps a misunderstanding of dictionary over a list. Commented Dec 22, 2017 at 6:33
  • 1
    @sonismit Why not find the length as you read it? df = pd.read_csv(join); print(len(df)); di[lst[count]] = df Commented Dec 22, 2017 at 6:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Use enumerate so you don't need an external count variable.
@cᴏʟᴅsᴘᴇᴇᴅ there are so many improvements that can be done here. I leave it to OP with this.
@Dark I am not clear about how to use enumerate. Can you provide me code line with this? Thanks in advance.
@Dark... Got it. Thanks. enumerate is useful when we need to increment counter itself. I will assign values to both count and files.
You got it. Happy coding. You will love python for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.