You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
=> that seems fine ^^ but in fact, when merging on=['a'], the index is ignored, so there are cases where the above fails.
better solution...
defchoose_non_null(val1, val2):
ifnp.isnan(val1):
returnval2else:
returnval1defselective_merge(df1, df2):
''' If there is a column in common, be greedy about getting more data, when there are NaNs, when interleaving, throw out the NaNs. '''common_cols=list(set(df1.columns.tolist()) &set(df2.columns.tolist()))
outdf=df1.merge(df2, how='outer', left_index=True, right_index=True
)
forcolincommon_cols:
left, right=col+'_x', col+'_y'outdf[col] =outdf.apply(
lambdarow: choose_non_null(row[left], row[right]), axis=1)
outdf=outdf.drop(labels=[left, right], axis=1)
returnoutdf