'My numpy select and where statement is leaving null values

I've tried to use both numpy select and where statements to develop another column in my dataframe. Neither is working and I'm not sure why.

import numpy as np
import pandas as pd

condition = [df_l['Policy Form'] == 'HO3',
             df_l['Policy Form'] == 'HO3C']
replace = [100000, 100000]
df_l['Personal Liability limit (08)'] = np.select(condition, replace, default = 50000)

I also tried:

df_l['Personal Liability limit (08)'] = np.where((df_l['Policy Form'] == 'HO3') | df_l['Policy Form'] == 'HO3C', 100000, 50000)

I'm not sure why either is working though.



Solution 1:[1]

You can do this without NumPy, directly in Pandas:

df_l['Personal Liability limit (08)'] = 50000
selection = (df_l['Policy Form'] == 'HO3') | (df_l['Policy Form'] == 'HO3C')
df_l.loc[selection, 'Personal Liability limit (08)'] = 100000

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