2

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.

2
  • Can you explain why you want empty DataFrames please. Commented Jul 4, 2019 at 5:18
  • @cs95 cause I want to assign values(type dataframe) to different names using a for loop. for example: var_names=[a1,b2,c3,d4,e5]. I want to use: for names in var_names to assign values to those variable from a much bigger dataframe Commented Jul 4, 2019 at 5:27

5 Answers 5

5

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))
Sign up to request clarification or add additional context in comments.

5 Comments

Then how do I copy an existed dataframe to those indexes
Let's say you have a dataframe in a variable x and you want to make the 1st element of dataframe list as that dataframe. Then you can do df_list[0] = x
thank you for the help. what if the indexes are strings not numbers. How do I do that?
Then you have to use a dict rather than a list where you can make use of string keys
Can you please provide some sample codes? I don't how to use dict.
1

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))

Comments

0

Another idea with itertools:

 from itertools import repeat
 a, b, c = repeat(pd.DataFrame({'col':[0.0]}), 3)

Comments

-1

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]

Comments

-1

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()

7 Comments

if I want to assign a value(type dataframe) to a, what should I do? df_list.index('a')=df?
you mean you want to copy an existing dataframe to a?
if you want to copy an existing dataframe, you dont need to even initialise. df_new = df, here df is an existing dataframe.
it will be better to provide reason for downgrade.
I know that. But if want to use a for loop on a list of none initialized dataframe, python won't allow me to do that. I modified the question a little bit. Hope it will make my question more clear.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.