Concatenate Pandas Dataframe
<p>I am trying to concatenate few dataframes which depends on if they are created in the process. So for example I have following master list of many data frame</p>

<pre><code> List=['df_facebook','df_LinkedIn','df_Insta','tweet_final','df_Google','df_Slide','df_Youtube']
</code></pre>

<p>Now In my process, assume df_google did not get created, so I wont be able to do </p>

<pre><code>df_Social = pd.concat([df_facebook,df_LinkedIn,df_Insta,tweet_final,df_Youtube,df_Google,df_Slide])
</code></pre>

<p>So to handle this, since I want it to completely automated &amp; Dynamic, I tried to something like this </p>

<pre><code>list_a=[col for col in List if col in locals()]
df_Social=pd.concat(list_a)
</code></pre>

<p>Which gives me an error</p>

<pre><code>TypeError: cannot concatenate a non-NDFrame object
</code></pre>

<p>Which is right also, Please help me to fix this, or some alternate solution, which can help me to just concatenate the already created dataframes dynamically. I will appreciate every help!</p>

<p>it is not a good idea to look variables in locals.
however, you can try:</p>

<pre><code>list_a=[locals()[col] for col in List if col in locals()]
# for example
List = ['df_google', 'df_facebook', 'df_linkedin']
# the frames should have the same column name if you want to concat vertically
df_google = pd.DataFrame(np.random.randn(20,3))
df_facebook = pd.DataFrame(np.random.randn(40, 3))
list_a=[locals()[col] for col in List if col in locals()]
pd.concat(list_a)
</code></pre>

python pandas for-loop dataframe