I have a dataframe
data_in = {'A':['A1', '', '', 'A4',''],
'B':['', 'B2', 'B3', '',''],
'C':['C1','C2','','','C5']}
df_in = pd.DataFrame(data)
print(df_in)
A B C
0 A1 C1
1 B2 C2
2 B3
3 A4
4 C5
I'm trying to replace A or B column if C column is not empty and A or B are not empty. After replacing, I need to clear value in C column.
I expect this output
A B C
0 C1
1 C2
2 B3
3 A4
4 C5
I tried several things, the closest is
df_in['A'] = np.where(
(df_in['A'] !='') & (df_in['C'] != '') , df_in['A'], df_in['C']
)
df_in['B'] = np.where(
(df_in['B'] !='') & (df_in['C'] != '') , df_in['B'], df_in['C']
)
But this clear also the other value and I l'm loosing A4 and B3 and I don't clear C1 and C2
What I got
A B C
0 C1 C1
1 C2 C2
2
3
4 C5
Thank you