Creating and modifying dataframes with a for loop python
The second method should be used in python. As the definition is in a clear place. Further the variables are scoped as much as possible. You can easily look for the dataframes which were defined by looking for the keys of the dictionary.
You can add a new column by indexing:
d = {}
for i in tablelist1:
d[i] = pd.DataFrame()
d[i]['Value'] = df1["profit"] - df1["tax"]
The first method creates global variables with the name given by the table array. Therefore if you would like to append new columns. You would not use table as the variable name of the dataframe, but the name given from the strings. Something again with exec. This way I do not recommend as variables are defined as globals, and the way they are defined is very hidden. See following:
import pandas as pd
tablelist = ['QTD','YTD','OneYear','Inception']
for table in tablelist:
exec('{} = pd.DataFrame()'.format(table))
exec(table + "['col1'] = []")
print(QTD) # or others
create multiple new data frames in a loop by subsetting another and adding a suffix
Your buckets_list
is simply a list of strings and not of dataframes. And if you really want to keep the names of the variables and add a prefix, have buckets_list
be a dictionary in the following format: {dataframe_variable_name: dataframe_object
, and iterate through it by using for key, val in buckets_list.items():
, where the key
will be the variable name and val
will be the dataframe object.
EDIT: for clarification and answering the OP's comment
buckets_list = {dataframe1_name: dataframe1,
dataframe2_name: dataframe2,
...}
Just fill it up with all the dataframes you want...
Create a dataframe using while loop in pandas
Pandas dataframes are normally used for manipulating existing data through unified operations on full columns/rows, not iterating over things, but you can accomplish the effect you're looking for by just generating the list normally and passing it into dataframe:
i = s['c'][1]
l = []
while True:
l.append(i)
i += 2
if i >= s['b'][1]:
l.append(s['b'][1])
break
e=pd.DataFrame(l)
How to create a dataframe with every iteration of a for loop
Use globals()
to create your variables dynamically. Use with caution, it's not really a good practice but it should work:
for i in range(100):
get_data = [data for data in cur]
df = pd.DataFrame(get_data)
globals()[f"df_{i}"] = df # <- replace your dict by globals()
Related Topics
How to Plot Multiple Pandas Columns
Most Pythonic Way to Kill a Thread After Some Period of Time
How to Skip Specific Indexes in an Array
How to Use Chrome Webdriver in Selenium to Download Files in Python
How to Pass Variables from Python Script to Bash Script
Pyspark: How to Duplicate a Row N Time in Dataframe
Move Files Between Two Aws S3 Buckets Using Boto3
Vscode Import Error for Python Module
Convert Images from [-1; 1] to [0; 255]
How to Download the Latest File of an S3 Bucket Using Boto3
Creating New Dataframes in Loop in Python
How to Execute Local Python Scripts in Jenkins Ui
How to Merge 2 CSV Files Together by Multiple Columns in Python
How to Extract X,Y Coordinates from Opencv "Cv2.Keypoint" Object