Reputation: 5152
I am trying to create multiple empty pandas dataframes in the following way:
dfnames = ['df0', 'df1', 'df2']
x = pd.Dataframes for x in dfnames
The above mentionned line returns error syntax. What would be the correct way to create the dataframes?
Upvotes: 2
Views: 4272
Reputation: 27869
If you want to create variables that contain empty DataFrames, this will do what you need:
dfnames = ['df0', 'df1', 'df2']
for x in dfnames: exec(x + ' = pd.DataFrame()')
Upvotes: 3
Reputation: 31
You can't have many data frames within a single variable name, here you are trying to save all empty data frames in x
. Plus, you are using wrong attribute name, it is pd.DataFrame
and not pd.Dataframes
.
I did this and it worked-
dfnames = ['df0', 'df1', 'df2']
x = [pd.DataFrame for x in dfnames]
Upvotes: 0
Reputation: 492
to connect the names in fdnames to the dataframes, use e.g. a dict:
dataFrames = {(name, pd.DataFrame()) for name in dfnames}
Upvotes: 2