I read several other posts about combining dataframes but my issue is a little different as I don't know the dataframe names in advance. Here's why:
I have a function that produces a data frame showing first purchases and subsequent purchases:
renew(df, 2).show()
+--------------+----------+----------+---------+----------+---------+
|First_Purchase|Renewal_Mo|second_buy|third_buy|fourth_buy|fifth_buy|
+--------------+----------+----------+---------+----------+---------+
|2 |2 |20 |4 |3 |2 |
|2 |48 |2 |0 |0 |0 |
|2 |24 |2 |0 |0 |0 |
|2 |12 |12 |2 |0 |0 |
|2 |6 |3 |1 |0 |0 |
+--------------+----------+----------+---------+----------+---------+
I can change the input to produce dataframe where First_Purchase = 12 (or 3, or 6 etc) and produce another dataframe:
renew(df, 12).show()
+--------------+----------+----------+---------+----------+---------+
|First_Purchase|Renewal_Mo|second_buy|third_buy|fourth_buy|fifth_buy|
+--------------+----------+----------+---------+----------+---------+
|12 |2 |12 |1 |0 |0 |
|12 |1 |9 |1 |0 |0 |
|12 |48 |1 |0 |0 |0 |
|12 |24 |7 |0 |0 |0 |
|12 |12 |64 |4 |0 |0 |
|12 |6 |6 |1 |0 |0 |
|12 |3 |4 |0 |0 |0 |
+--------------+----------+----------+---------+----------+---------+
I need to loop though various First_Purchase values and create one dataframe. e.g.,
month = [2, 12]
for x in month
renew_modified(all_renewals_cleaned_md, x)
The code above will loop through but overwrite the previous dataframe leaving me with only the last dataframe rather than append them.
If I could export each dataframe in the loop to a new and uniquely named dataframe I could concatenate them like this:
purchases_combined = [df1,df12,df3,df6]
But I haven't been able to come up with the code to loop through, export the dataframe to unique names dynamically, and then combine them so they would look like this:
+--------------+----------+----------+---------+----------+---------+
|First_Purchase|Renewal_Mo|second_buy|third_buy|fourth_buy|fifth_buy|
+--------------+----------+----------+---------+----------+---------+
|2 |2 |20 |4 |3 |2 |
|2 |48 |2 |0 |0 |0 |
|2 |24 |2 |0 |0 |0 |
|2 |12 |12 |2 |0 |0 |
|2 |6 |3 |1 |0 |0 |
|12 |2 |12 |1 |0 |0 |
|12 |1 |9 |1 |0 |0 |
|12 |48 |1 |0 |0 |0 |
|12 |24 |7 |0 |0 |0 |
|12 |12 |64 |4 |0 |0 |
|12 |6 |6 |1 |0 |0 |
|12 |3 |4 |0 |0 |0 |
+--------------+----------+----------+---------+----------+---------+
Does anyone have any suggestions?