How to create multiple empty dataframes?

Question:

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 empty dataframe? Because eventually I want to use for loop to assign dataframe values to

var_names=[a,b,c,d,e]

Basically, I need to assign values from a much bigger dataframe to lots of small dataframe with targeted names(possible complicated names, just for easy understanding)

variables=[ag_2018,al_2018,au_2018,bu_2018,cu_2018,fu_2018,hc_2018,
           ni_2018,pb_2018,rb_2018,ru_2018,sn_2018,sp_2018,wr_2018,
           zn_2018]

for var in variables:
    var=(a portion of a much bigger dataframe)

These are my codes. Python won’t allow me to do it showing error: ag_2018 is not defined.

I saw some suggestions using dict, can someone please provide more detail about how to apply it since I am not very familiar with dict. Thanks.

Asked By: Waynexu

||

Answers:

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 = 10

df_list = [pd.DataFrame() for _ 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 in range(10))
Answered By: Sreeram TP

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]

Answered By: chang_trenton

You can try below two line code.

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

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))
Answered By: Joe

Another idea with itertools:

 from itertools import repeat
 a, b, c = repeat(pd.DataFrame({'col':[0.0]}), 3)
Answered By: Marcel Flygare
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.