0

I've following dataframes.

import pandas as pd
from pprint import pprint

df1 = pd.DataFrame(columns=["A", "B"])
df1["A"] = [1, 2]
df1["B"] = ["A", "B"]

df2 = pd.DataFrame(columns=["A", "C"])
df2["A"] = [3, 4]
df2["C"] = ["C", "D"]

df3 = pd.DataFrame(columns=["A", "C"])
df3["A"] = ["E", "F"]
df3["C"] = [0.1, 0.2]

I want to merge all three dataframes into single dataframe. Expected Output:

df

 A B  C  D
 1 A
 2 B
 3    C  
 4    D
   E     0.1
   F     0.2

I tried using merge function . It appends only 2 dataframes and also appends characters to column names, which I don't intend to do.

df = df1.merge(df2, left_on='A', right_on='C')

I'd like to know if there is an alternative way or in build function to do this.

3
  • There is a mistake in df3. Should be df3 = pd.DataFrame(columns=["B", "D"]) df3["B"] = ["E", "F"] df3["D"] = [0.1, 0.2] Commented Aug 1, 2019 at 14:55
  • This mistake is present in all dfs. Your strings (the letters) need to be put in parantheses. Commented Aug 1, 2019 at 14:56
  • Yep, that and the letters in df3 are simply not correct Commented Aug 1, 2019 at 14:58

3 Answers 3

1
df = pd.concat([df1,df2,df3],sort=False).fillna('')
Sign up to request clarification or add additional context in comments.

Comments

1

You are looking for pd.concat

pd.concat([df1,df2,df3])
    A   B   C   D
0   1.0 A   NaN NaN
1   2.0 B   NaN NaN
0   3.0 NaN C   NaN
1   4.0 NaN D   NaN
0   NaN E   NaN 0.1
1   NaN F   NaN 0.2

Comments

1

Is it

pd.concat((df1,df2,df3), sort=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.