How to Name Dataframes Dynamically in Python

Create new dataframe in pandas with dynamic names also add new column

Creating variables with dynamic names is typically a bad practice.

I think the best solution for your problem is to store your dataframes into a dictionary and dynamically generate the name of the key to access each dataframe.

import copy

dict_of_df = {}
for ym in [201511, 201612, 201710]:

key_name = 'df_new_'+str(ym)

dict_of_df[key_name] = copy.deepcopy(df)

to_change = df['YearMonth']< ym
dict_of_df[key_name].loc[to_change, 'new_col'] = ym

dict_of_df.keys()
Out[36]: ['df_new_201710', 'df_new_201612', 'df_new_201511']

dict_of_df
Out[37]:
{'df_new_201511': A B ID t YearMonth new_col
0 -a a 1 2016-12-05 07:53:35.943 201612 201612
1 1 NaN 2 2016-12-05 07:53:35.943 201612 201612
2 a c 2 2016-12-05 07:53:35.943 201612 201612,
'df_new_201612': A B ID t YearMonth new_col
0 -a a 1 2016-12-05 07:53:35.943 201612 201612
1 1 NaN 2 2016-12-05 07:53:35.943 201612 201612
2 a c 2 2016-12-05 07:53:35.943 201612 201612,
'df_new_201710': A B ID t YearMonth new_col
0 -a a 1 2016-12-05 07:53:35.943 201612 201710
1 1 NaN 2 2016-12-05 07:53:35.943 201612 201710
2 a c 2 2016-12-05 07:53:35.943 201612 201710}

# Extract a single dataframe
df_2015 = dict_of_df['df_new_201511']

Dynamically setting dataframe name while reading csv files from a folder in python

globals() returns a dictionary of current module's variables. Instead of creating a dictionary in your first approach, you can create a new variable dynamically by creating a key on globals():

for f in glob.glob(os.path.join(path, '*.csv'):
variable_name = os.path.splitext(os.path.basename(f))[0]
globals()[variable_name] = pd.read_csv(f)

How to name dataframes dynamically in Python?

You could use something like this:

for s in sheets:
vars()['df'+ s] = pd.read_excel(xls, sheet_name=s)


Related Topics



Leave a reply



Submit