如果存在第二列,则填充第二列值,否则填充 Dataframe 中的第一列值

If there is a second column present then populate second column values, else populate first column values in Dataframe

我有一个如下所示的数据框:

col_a1, col_a2, col_b1, col_b2
 abc                     lmn
 def     ghi             qrs
 zxv                     vbn
 pej             iop     qaz
 eki     lod     yhe     wqe

我现在需要两列,A列和B列。条件总结:

Column A = col_a2 if col_a2 is present else col_a1  
Column B = col_a1 if col_a1 is present else col_b2

所需的数据框应如下所示:

 Column A Column B
    abc    lmn
    ghi    qrs
    zxv    vbn
    pej    iop
    lod    yhe

尝试:

df['A'] = df.apply(lambda x: x['col_a2'] if x['col_a2'] != '' else x['col_a1'], axis=1)
df['B'] = df.apply(lambda x: x['col_b1'] if x['col_b1'] != '' else x['col_b2'], axis=1)
print(df[['A', 'B']])

    A       B
0   abc     lmn
1   ghi     qrs
2   zxv     vbn
3   pej     iop
4   lod     yhe

如果单元格中确实没有任何内容(与 NaN 等相反),!='' 将起作用。如果您有实际的 NaN 值,请使用:

df['A'] = df.apply(lambda x: x['col_a2'] if pd.notna(x['col_a2']) else x['col_a1'], axis=1)
df['B'] = df.apply(lambda x: x['col_b1'] if pd.notna(x['col_b1']) else x['col_b2'], axis=1)