Reputation: 57
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.
Upvotes: 2
Views: 5593
Reputation: 11907
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))
Upvotes: 5
Reputation: 887
Another idea with itertools:
from itertools import repeat
a, b, c = repeat(pd.DataFrame({'col':[0.0]}), 3)
Upvotes: 0
Reputation: 889
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))
Upvotes: 1
Reputation: 4486
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()
Upvotes: -1
Reputation: 915
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]
Upvotes: -1