Search code examples
pythonpandasdataframeconcatenation

How do i keep empty columns while concating in pandas?


So I have 2 dataframes both with empty columns but that is intentional. So how do i keep the empty columns when i concat them on the same axis?

abdc  123  *nan*

1     2    *nan*

Both the dfs look similar. So for eg, pd.concat([df,df]) removes the empty column I want the df to look like this

abdc  123  *nan*  abdc  123  *nan*

1     2    *nan*  1     2    *nan*

So is this possible to do?


Solution

  • df=pd.DataFrame({'abcd':[1],"123":[2],"*nan*":[np.nan]})
    df
       abcd  123  *nan*
    0     1    2    NaN
    
    pd.concat([df,df],axis=1)
       abcd  123  *nan*  abcd  123  *nan*
    0     1    2    NaN     1    2    NaN