Add length of multiple dataframe and store it in a list

Question:

I have few dfs:

A
id A B C
1  2 2 2
2  3 3 3

B
id A B C
1  5 5 5
2  6 6 6
3  8 8 8
4  0 0 0

C 
id A B C
1  6 6 6

I need to find the length of each df and store it in a list:

search_list = ["A", "B", "C"]

I took the reference from the previous post. Is there a way to loop over this list to do something like:

my_list=[]
for i in search_list:
    my_list.append(len(search_list[i]))

Desired output:

len_df =

[{'A': 2},
 {'B': 4},
 {'C': 1}]
Asked By: sloth14

||

Answers:

You can loop over the DataFrames themselves if their are "loopable"/iterable, meaning are in a list or other similar container themselves. And since those DataFrames don’t contain their names you want for them, you need a separate list/container of their names like search_list, which you mentioned.
If df_a, df_b, and df_c are the names of the DataFrame variables and search_list is a list of their names, then:

df_list = [df_a, df_b, df_c]
len_list = [{search_list[i] : df.shape[0]} for i, df in enumerate(df_list)]

But if you want to keep those DataFrame names together with the DataFrames themselves in your code for further use, it might be reasonable to initially organize those DataFrames not in a list, but in a dictionary with their names:

df_dict = { 'A': df_a, 'B': df_b, 'C': df_c }
len_list = [{key : df.shape[0]} for key, df in df_dict.items()]
Answered By: Alex

They need to be in a list or dict in the first place if the intention is to loop thru the list.

Here is how you would do this:

import pandas as pd

# 3 dicts
a = {'a':[1,2,3], 'b':[4,5,6]}
b = {'a':[1,2,3,4], 'b':[4,5,6,7]}
c = {'a':[1,2,3,4,5], 'b':[4,5,6,7,8]}

# 3 dataframes
df1=pd.DataFrame(a)
df2=pd.DataFrame(b)
df3=pd.DataFrame(c)

# dict or dataframes
dict_of_df = {'a':df1, 'b':df2, 'c':df3}


# put all lengths into a new dict
df_lenghts = {}
for k,v in dict_of_df.items():
    df_lenghts[k] = len(v)


print(df_lenghts)

And this is the result:

{'a': 3, 'b': 4, 'c': 5}
Answered By: D.L
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.