2

I have one data frame (df1) with 5 columns and another (df2) with 10 columns. I want to add columns from df2 to df1, but only columns names (without values). Also, I want to do the same with adding columns without values from df1 to df2.

Here are the data frames:

df1

  A      B     C      D     E     
  1      234   52     1     54
  54     23    87     5     125
  678    67    63     8     18
  45     21    36     5     65
  8      5     24     3     13

df2

  F      G     H      I     J      K      L     M      N     O     
  12     34    2      17    4      19     54    7      58    123
  154    3     7      53    25     2      47    27     84    6
  78     7     3      82    8      56     21    29     547   1

And I want to get this:

df1

  A      B     C      D     E      F      G      H      I      J      K      L      M      N      O     
  1      234   52     1     54
  54     23    87     5     125
  678    67    63     8     18
  45     21    36     5     65
  8      5     24     3     13

And I want to get this:

df2

  A       B       C       D      E      F      G     H      I     J      K      L     M      N     O     
                                        12     34    2      17    4      19     54    7      58    123
                                        154    3     7      53    25     2      47    27     84    6
                                        78     7     3      82    8      56     21    29     547   1

I tried with df.columns.values and got the array of columns names, but then I have to apply them as data frame columns and give them empty values, and the way that I am doing now has too many lines of code, and I just wonder is it some easier way to do that? I will appreciate any help.

1 Answer 1

6

Use Index.union with DataFrame.reindex:

cols = df1.columns.union(df2.columns)
#if order is important
#cols = df1.columns.append(df2.columns)

df1 = df1.reindex(columns=cols)
df2 = df2.reindex(columns=cols)

print (df1)
     A    B   C  D    E   F   G   H   I   J   K   L   M   N   O
0    1  234  52  1   54 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1   54   23  87  5  125 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2  678   67  63  8   18 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3   45   21  36  5   65 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4    8    5  24  3   13 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

print (df2)
    A   B   C   D   E    F   G  H   I   J   K   L   M    N    O
0 NaN NaN NaN NaN NaN   12  34  2  17   4  19  54   7   58  123
1 NaN NaN NaN NaN NaN  154   3  7  53  25   2  47  27   84    6
2 NaN NaN NaN NaN NaN   78   7  3  82   8  56  21  29  547    1

If same index values in both DataFrames is possible use DataFrame.align:

print (df1)
     A    B   C  D    E
0    1  234  52  1   54
1   54   23  87  5  125
2  678   67  63  8   18

df1, df2 = df1.align(df2)
print (df1)
     A    B   C  D    E   F   G   H   I   J   K   L   M   N   O
0    1  234  52  1   54 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1   54   23  87  5  125 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2  678   67  63  8   18 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

print (df2)
    A   B   C   D   E    F   G  H   I   J   K   L   M    N    O
0 NaN NaN NaN NaN NaN   12  34  2  17   4  19  54   7   58  123
1 NaN NaN NaN NaN NaN  154   3  7  53  25   2  47  27   84    6
2 NaN NaN NaN NaN NaN   78   7  3  82   8  56  21  29  547    1
Sign up to request clarification or add additional context in comments.

5 Comments

Nice. +1, also can use .fillna('') to replace with empty as OP's desired output.
@jezrael Is it possible to keep the same order of columns, not sorted? For example, if in df2 the order of columns is like this: J, F, O, G, M, H, N, L, K, I. What if I want to keep that order of columns? Not to use reindex?
@U9-Forward - Thank you. Yes, if OP need it.
@jezrael Happy to add :-)
@slobokv83 - if order is important then use cols = df1.columns.append(df2.columns), added to answer.

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.