Save a new dataframe for every iteration of for loop
I need to save each iteration results as a new list or Dataframe. As of Now I m only able to print the last iteration of the loop.I need each iteration or array saved in Dataframe.
for unique_age in sorted(Data.Age.unique()):
df_result = Data.query(F"Age == {unique_age}")
df_result['Percentile']=df_result.Total_Revenue.rank(pct=True)*100
data_list.append(df_result)
df_result
[Data:][1] [Expected output:][2] [1]: https://i.stack.imgur.com/gshKe.png [2]: https://i.stack.imgur.com/R9zpU.png
CodePudding user response:
Right now you are adding each dataframes into a list as each element. It is a good approach, but the dataframes will not be named. So, instead use the dictionary and keep names as keys, and dataframes as values.
data_list = {}
for unique_age in sorted(Data.Age.unique()):
df_result = Data.query(F"Age == {unique_age}")
df_result['Percentile']=df_result.Total_Revenue.rank(pct=True)*100
name = "age" str(unique_age)
data_list[name] = df_result
Afterwards you can call by their names such as data_list["age10"]
or something like that, instead of calling them by indexes data_list[1]
.
Or if you are feeling spicy, may be this is something you would be interested in. This creates new variables (here dataframes) in each loops
for unique_age in sorted(Data.Age.unique()):
df_result = Data.query(F"Age == {unique_age}")
df_result['Percentile']=df_result.Total_Revenue.rank(pct=True)*100
exec('df_{}age = df_result'.format(unique_age))
CodePudding user response:
need to have all iterated rows not only the last one