ðŸ“Ž

# Use a column value if not Null, or another column from a pandas DataFrame

When calculating a new column in a DataFrame, if you want to use the value of column if it is not Null, or fall back on the value of another column, you can do it with a one-liner.

``````# Import libraries
import pandas as pd
import numpy as np

# Create a sample DataFrame
df = pd.DataFrame(np.random.random(size=(10, 2)), columns=list('AB')).round(3)
df.iloc[[2,3,5,8], 0] = np.nan
df``````
 A B 0 0.591 0.394 1 0.573 0.374 2 NaN 0.309 3 NaN 0.254 4 0.231 0.230 5 NaN 0.099 6 0.392 0.703 7 0.447 0.496 8 NaN 0.058 9 0.597 0.658

Here is the code:

``````# Create a third column, that will take column A value if not Null
# or fall back on B
df['C'] = df['A'].fillna(df['B'])
df``````
 A B C 0 0.591 0.394 0.591 1 0.573 0.374 0.573 2 NaN 0.309 0.309 3 NaN 0.254 0.254 4 0.231 0.230 0.231 5 NaN 0.099 0.099 6 0.392 0.703 0.392 7 0.447 0.496 0.447 8 NaN 0.058 0.058 9 0.597 0.658 0.597

You can even chain it, to get a third column value, if the first two columns are Null:

``````# Add some data
df['C'] = np.random.random(size=(10, 1)).round(3)
df.iloc[[1,4,5,8], 1] = np.nan

# Create a fourth column, that would take column C value
# if the first two are Null
df['D'] = df['A'].fillna(df['B']).fillna(df['C'])
df``````
 A B C D 0 0.591 0.394 0.637 0.591 1 0.573 NaN 0.331 0.573 2 NaN 0.309 0.738 0.309 3 NaN 0.254 0.856 0.254 4 0.231 NaN 0.160 0.231 5 NaN NaN 0.926 0.926 6 0.392 0.703 0.847 0.392 7 0.447 0.496 0.824 0.447 8 NaN NaN 0.529 0.529 9 0.597 0.658 0.948 0.597