Skip to content Skip to sidebar Skip to footer

How To Create Multiple Empty Dataframes?

Instead of doing: a=pd.DataFrame() d=pd.DataFrame() c=pd.DataFrame() d=pd.DataFrame() e=pd.DataFrame() each at a time. Is there a quick way to initialize all variables with em

Solution 1:

Let's say you have to make n empty dataframes and put it in a list, you can do something like this with the help of list comprehension.

n = 10df_list = [pd.DataFrame() for x in range(n)]

You can do similar with a dict so that you can make use of non int keys,

import pandas as pd
df_dict = dict(('df_' + str(x), pd.DataFrame()) for x inrange(10))

Solution 2:

If you want to use dictionaries:

df_names = ['a', 'b', 'c', 'd']
df_list = [pd.DataFrame() for df in df_names]

Then typecast a dictionary using the two lists by using dict() and zip() by:

df_dict = dict(zip(df_names, df_list))

Solution 3:

If you're looking for a list of DataFrames, you should be able to do that with a list comprehension like so: [pd.Dataframe() for var in var_names]

Solution 4:

You can try below two line code.

import pandas as pddf_list= ['a', 'b', 'c', 'd', 'e']
for i in df_list:
    i = pd.DataFrame()

Post a Comment for "How To Create Multiple Empty Dataframes?"