JamAndJammies JamAndJammies - 1 year ago 150
Python Question

How can I merge columns with NaN with adjacent columns that has values in pandas

One column has NaN and some values, the other columns also has NaN and some values. It is not possible for both columns to have values but it is possible for both columns to have NaN. Is there a way I can merge the columns together?
I've tried selecting one column and

df.fillna
with a forumla, that doesn't work.

quad_data['new'] = quad_data.apply(lambda x: function(x.a, x.b, const_a, const_b), axis=1)
df1 = pd.merge(df1, quad_data[['a','b','new']], left_on=['a','b'], right_on = ['a','b'], how='inner')


new_x new_y
0 NaN 0.997652
1 NaN 0.861592
2 0 NaN
3 0.997652 NaN
4 0.861592 NaN
5 2.673742 NaN
6 2.618845 NaN
7 NaN 0.432525
8 NaN NaN
9 0.582576 NaN
10 0.50845 NaN
11 NaN 0.341510
12 NaN 0.351510
13 1.404787 NaN
14 2.410116 NaN
15 0.540265 NaN
16 NaN 1.404787
17 NaN 2.410116
18 NaN 0.540265
19 NaN 1.403903
20 1.448987 NaN

Answer Source

combine_first and fillna are good alternatives in general, but these alternatives work since your NaNs are exclusive.

Option 1
df.max

s = quad_data.max(1)
print(s)
0     0.997652
1     0.861592
2     0.000000
3     0.997652
4     0.861592
5     2.673742
6     2.618845
7     0.432525
8          NaN
9     0.582576
10    0.508450
11    0.341510
12    0.351510
13    1.404787
14    2.410116
15    0.540265
16    1.404787
17    2.410116
18    0.540265
19    1.403903
20    1.448987
dtype: float64

Option 2
df.sum

s = quad_data.sum(1)
print(s)
0     0.997652
1     0.861592
2     0.000000
3     0.997652
4     0.861592
5     2.673742
6     2.618845
7     0.432525
8          NaN
9     0.582576
10    0.508450
11    0.341510
12    0.351510
13    1.404787
14    2.410116
15    0.540265
16    1.404787
17    2.410116
18    0.540265
19    1.403903
20    1.448987
dtype: float64

quad_data['new'] = s 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download