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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With