singularity2047
singularity2047

Reputation: 1071

How to create a list of non-empty dataframes names?

I have created a list of dataframe and want to run a loop through the list to run some manipulation on some of those dataframes. Note that although this is a list I have manually created, but this dataframes exist in my code.

df_list = [df_1, df_2, df_3, df_4, df_5, ...]

list_df_matching = []
list_non_matching = []

Most of these dataframes are blank. But 2 of them will have some records in them. I want to find the name of those dataframes and create a new list - list_non_matching

for df_name in df_list:
    q = df_name.count()
    if q > 0:
        list_non_matching.append(df_name)
    else:
        list_df_matching.append(df_name)

My goal is to get a list of dataframe names like [df_4, df_10], but I am getting the following:

[DataFrame[id: string, nbr: string, name: string, code1: string, code2: string],
 DataFrame[id: string, nbr: string, name: string, code3: string, code4: string]]

Is the list approach incorrect? Is there a better way of doing it?

Upvotes: 1

Views: 251

Answers (1)

Laurent
Laurent

Reputation: 13518

Here is an example to illustrate one way to do it with the help of empty property and Python built-in function globals:

import pandas as pd

df1 = pd.DataFrame()
df2 = pd.DataFrame({"col1": [2, 4], "col2": [5, 9]})
df3 = pd.DataFrame(columns = ["col1", "col2"])
df4 = pd.DataFrame({"col1": [3, 8], "col2": [2, 0]})
df5 = pd.DataFrame({"col1": [], "col2": []})

df_list = [df1, df2, df3, df4, df5]
list_non_matching = [
    name
    for df in df_list
    for name in globals()
    if not df.empty and globals()[name] is df
]
print(list_non_matching)
# Output
['df2', 'df4']

Upvotes: 1

Related Questions