0
votes

I have two Pandas data frames. df1 has columns ['a','b','c'] and df2 has columns ['a','c','d']. Now, I create a new data frame df3 with columns ['a', b','c','d'].

I want to fill df3 with all the inputs from df1 and df2. For example, if I have x rows in df1, and y rows in df2, then I will have x+y rows in df3.

Which Pandas function fills the new dataframe based on partial columns?

3
Thank you! But, when I merge I need to join on something right? What I want is to add all the rows one by one, and write values to corresponding columns. - independentvariable

3 Answers

3
votes

Example data:

df1 = pd.DataFrame({'a':[1, 2, 3], 'b':[2, 3, 4], 'd':['h', 'j', 'k']})
df2 = pd.DataFrame({'a':[5, 6, 7], 'b':[1, 1, 1], 'c':[2, 2, 2]})

Code:

df1.append(df2)

Out:

   a  b    c    d
0  1  2  NaN    h
1  2  3  NaN    j
2  3  4  NaN    k
0  5  1  2.0  NaN
1  6  1  2.0  NaN
2  7  1  2.0  NaN
1
votes

use append function of dataframe https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html

anotherFrame = df1.append(df2, ignore_index=True)

another way is merge - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html

df1.merge(df2, how='outer')
1
votes

How about:

df1 =  pd.DataFrame({"a": [1,2], "b": [3,4], "c": [5,6]})
df2 =  pd.DataFrame({"a": [7,8], "c": [9,10], "d": [11,12]})
df3 = df1.append(df2, sort=False)
df3
   a    b   c     d
0  1  3.0   5   NaN
1  2  4.0   6   NaN
0  7  NaN   9  11.0
1  8  NaN  10  12.0