lassence.net

Field notes from the AI frontier

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

--- archive: codebook, 2020–2023 · topic: Pandas ---

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
AB
00.5910.394
10.5730.374
2NaN0.309
3NaN0.254
40.2310.230
5NaN0.099
60.3920.703
70.4470.496
8NaN0.058
90.5970.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
ABC
00.5910.3940.591
10.5730.3740.573
2NaN0.3090.309
3NaN0.2540.254
40.2310.2300.231
5NaN0.0990.099
60.3920.7030.392
70.4470.4960.447
8NaN0.0580.058
90.5970.6580.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
ABCD
00.5910.3940.6370.591
10.573NaN0.3310.573
2NaN0.3090.7380.309
3NaN0.2540.8560.254
40.231NaN0.1600.231
5NaNNaN0.9260.926
60.3920.7030.8470.392
70.4470.4960.8240.447
8NaNNaN0.5290.529
90.5970.6580.9480.597