New to Python here. I hope my question isn't entirely redundant - if it is, let me know and chalk it up to my inexperience with StackOverflow.
In any case, I'm working with the Titanic dataset from kaggle.com, and I'm looking to use a set of conditional statements to replace NaN 'values' throughout the Age column of the dataframe. Ultimately, I'd like to generate results based on the following conditions: 1) if age==NaN, and Title==(X or Y or Z), generate a random number in the 0-18 range 2) if age==NaN, and Title==(A or B or C), generate a random number in the 19-80 range
Note: 'Title' is a column with the title of individual listed (i.e. Mr., Mrs., Lord, etc.)
I found a similar situation here, but I haven't been able to adapt it to my case as it doesn't approach conditionality at all.
Here is my most recent attempt (per. the replies as this update)
Attempt 1
import random
mask_young = (df.Age.isnull()) & (df.Title.isin(Title_Young))
df.loc[mask_young, 'Age'] = df.loc[mask_young, 'Age'].apply(lambda x: np.random.randint(0,18))
mask_old = (df.Age.isnull()) & (df.Title.isin(Title_Old))
df.loc[mask_old, 'Age'] = df.loc[mask_old, 'Age'].apply(lambda x: np.random.randint(18,65))
mask_all = (df.Age.isnull()) & (df.Title.isin(Title_All))
df.loc[mask_all, 'Age'] = df.loc[mask_all, 'Age'].apply(lambda x: np.random.randint(0,65))
Result is no error, but no correction to NaN values in 'Age' column