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
How to End Program If Input == "Quit" With Many If Statements
What Does Sqlite3.Operationalerror: Near "-": Syntax Error Mean
How to Delete All Columns in Dataframe Except Certain Ones
How to Find Duration Between Two Time Difference in Python Dataframe
How to Split a Byte String into Separate Bytes in Python
Python Get File Size of Volumes or Physical Drives
How to Read a CSV File from an S3 Bucket Using Pandas in Python
Print the Student Name and the Score of Student in Python3
Add Numpy Array as Column to Pandas Data Frame
Valueerror: Time Data Does Not Match Format When Parsing a Date
How to Extract Column Value Within Square Brackets in Pyspark
How to Get Slope from Timeseries Data in Pandas
Matplotlib: Drawing Lines Between Points Ignoring Missing Data
Split/Explode a Column of Dictionaries into Separate Columns With Pandas
How to Add a Path to Pythonpath in Virtualenv
How to Merge Columns from Multiple CSV Files Using Python