cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Append an empty dataframe to a list of dataframes using for loop in python

Jack
New Contributor II

I have the following 3 dataframes:

imageI want to append df_forecast to each of df2_CA and df2_USA using a for-loop. However when I run my code, df_forecast is not appending: df2_CA and df2_USA appear exactly as shown above.

Here’s the code:

df_list=[df2_CA, df2_USA] 
 
for x in df_list:
     pd.concat([x, df_forecast]) 
     x['prod'].fillna(method='ffill',inplace=True)     
     x['country'].fillna(method='ffill',inplace=True)
     x.loc['2020-03':'2020-05', 'rev'] = 200

And here is the desired result:

imageWhat's wrong with my code?

2 REPLIES 2

User16764241763
Honored Contributor

@Jack Homareau​  Can you try union functionality with dataframes?

https://sparkbyexamples.com/pyspark/pyspark-union-and-unionall/

and then try to fill NaNs with the desired values?

Kaniz
Community Manager
Community Manager

Hi @Jack Homareau​ , We haven’t heard from you on the last response from @Arvind Ravish​ , and I was checking back to see if you have a resolution yet. If you have any solution, please do share that with the community as it can be helpful to others. Otherwise, we will respond with more details and try to help.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.