cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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?

1 REPLY 1

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?

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group