I'm trying to create a new column from 4 multiple columns that start with the same name. Either 3 or 4 out of these columns are NaN. I'd like to have the new column to have this non-NaN value, if it exists in the particular row. Otherwise the new column should be NaN.
Start with:
NaN = np.NaN
pd.DataFrame( {'process_time_1': [5, NaN, NaN, NaN], 'process_time_2': [NaN, NaN, NaN, NaN],
'process_time_3': [NaN, NaN, 3, 4], 'process_time_4': [NaN, NaN, NaN, NaN]} )
if would like the new column to look like:
NaN = np.NaN
pd.DataFrame( {'process_time': [5, NaN, 3, 4]} )
The code I currently have:
cols = [df1.columns.str.startswith('process')]
df1[cols][df1.notna()]
I'm breaking my head of this, would be great if somebody could help into the right direction:) Thanks in advance !