3

If I have one dataframe (df) with:

Columnx      Columny
1             NA
2             NA
3             NA
NA            4
NA            5
NA            6

and want

Column z
1
2
3
4
5
6

How can I do this in the simplest way possible? Essentially x and y are date columns, but due to a previous merge I have two date columns each filled with NA where the other is occupied, but only want one date column.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Josh Gale
  • 39
  • 4

2 Answers2

4

Use df.combine_first():

In [58]: df
Out[58]: 
   Column x  Column y
0       1.0       NaN
1       2.0       NaN
2       3.0       NaN
3       NaN       4.0
4       NaN       5.0
5       NaN       6.0

In [59]: df['Column x'].combine_first(df['Column y'])
Out[59]: 
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    6.0
Name: Column_x, dtype: float64
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
3

You could use stack here:

pd.DataFrame(df.stack().to_numpy(), columns=['Columnsz'])

   Columnsz
0       1.0
1       2.0
2       3.0
3       4.0
4       5.0
5       6.0
yatu
  • 86,083
  • 12
  • 84
  • 139