I have several columns in my DataFrame that can contain null values. I need to add a column that will check these specific columns and display first non-null value. If all values in those columns are null, then leave it null.
Example (please note that I only need select columns, original DF has columns that are present but not important for this example).
data = [[None, 20, None],[30, None, 30],[10, None, None]]
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
A B C
0 NaN 20.0 NaN
1 30.0 NaN 30.0
2 10.0 NaN NaN
Below is my attempt that looks up all columns in a row, but I need it only to look at values from specific columns (lets call them A, B, and C from example):
df['D'] = df.bfill(axis=1).iloc[:, 0]
print(df)
A B C D
0 NaN 20.0 NaN 20.0
1 30.0 NaN 30.0 30.0
2 10.0 NaN NaN 10.0