'Creating a column or list comprehension with multiple column conditions

I have a dataframe (sample) as under:

    col0    col1    col2    col3
0    101       3       5    
1    102       6       2       1
2    103       2    
3    104       4       6       4
4    105       8       3
5    106       1
6    107

Now I need two things as new columns to the same dataframe (col4 and col5):

  1. To bring latest value as per priority col3>col2>col1 for each row:

If col3 has value, col3, elif col2 has value, col2, elif col1 has value, col1, else "Invalid"

  1. To know whether that row has 1/2/3 or no values against these columns.

If col3 has value, 3, elif col2 has value, 2, elif col1 has value, 1, else 0.

I have done list comprehensions in format [x1 if condition1 else x2 if condition2 else x3 for val in df['col']]

However, I do not understand how to loop through three columns in single list comprehension attempt.

Or if there is some other way than list comprehension to do this?

I tried this:

df['col4'] = [df['col3'] if df['col3'].notna() else df['col2'] if df['col2'].notna() else df['col1'] if df['col1'].notna() else "Invalid" for x in df['col0']]
df['col5'] = [3 if df['col3'].notna() else 2 if df['col2'].notna() else 1 if df['col1'].notna() else 0]

But they do not work.



Solution 1:[1]

One solution that I tried was as under, but it requires four lines of code for each column:

df.loc[df['col1'].notna(),['col5']] = 1
df.loc[df['col2'].notna(),['col5']] = 2
df.loc[df['col3'].notna(),['col5']] = 3
df['col5'] = df['col5'].fillna(0)

Please suggest if any other means is possible.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1