OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Combining two Pandas dataframe with unequal number of columns (superset) [duplicate]

  • Thread starter Thread starter Bogaso
  • Start date Start date
B

Bogaso

Guest
I have below code:

Code:
import pandas as pd
df1 = pd.DataFrame({'a':[1,2,3],'y':[7,8,9]})
df2 = pd.DataFrame({'b':[10,11,12],'x':[13,14,15],'y':[16,17,18]})
pd.DataFrame(np.vstack([df1, df2]), columns=df1.columns)

Above code generates error. I expect that final dataframe will be all columns from df1 and df2. Therefore will be missing values as column names are different.

Is there any way to achieve this?
<p>I have below code:</p>
<pre><code>import pandas as pd
df1 = pd.DataFrame({'a':[1,2,3],'y':[7,8,9]})
df2 = pd.DataFrame({'b':[10,11,12],'x':[13,14,15],'y':[16,17,18]})
pd.DataFrame(np.vstack([df1, df2]), columns=df1.columns)
</code></pre>
<p>Above code generates error. I expect that final dataframe will be all columns from <code>df1</code> and <code>df2</code>. Therefore will be missing values as column names are different.</p>
<p>Is there any way to achieve this?</p>
 

Latest posts

Top