0

Df 1 has columns A B C D, Df2 has columns A B D. Df1 and Df2 are in a list. How do I concatenate them into 1 df?

Or can I directly append these dfs to one single df without using a list ?

ssr
  • 69
  • 6
  • IIUC you're just looking for a merge, `Df1.merge(Df2)` – yatu Aug 31 '19 at 18:12
  • list_of_dfs=[] for i in cc : df8 = *something* list_of_dfs.append(df8) print(list_of_dfs) This is how I'm generating the dfs. I want to bind those dfs by rows. One df should be added below the previous df and the whole output will be in a single df. – ssr Aug 31 '19 at 18:22
  • Possible duplicate of [Combine two pandas Data Frames (join on a common column)](https://stackoverflow.com/questions/18792918/combine-two-pandas-data-frames-join-on-a-common-column) – Tarun Kolla Aug 31 '19 at 18:26

1 Answers1

1

Short answer: yes you can combine them into single pandas dataframe without that much work. Sample code:

import pandas as pd

df1 = [(1,2,3,4)]
df2 = [(9,9,9)]

df1 = pd.DataFrame(df1, columns=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame(df2, columns=['A', 'B', 'D'])

df = pd.concat([df1, df2], sort=False)

Which results into:

>>> pd.concat([df1, df2], sort=False)
   A  B    C  D
0  1  2  3.0  4
0  9  9  NaN  9
Richard Nemeth
  • 1,784
  • 1
  • 6
  • 16