SettingWithCopyWarning when trying to classify users in pandas dataframe
I am getting a SettingWithCopyWarning when trying to classify users in pandas dataframe,
I have a Dataframe that contains a 'user_id' column, that if it is contained in a determined list, it will give me a value and if not, then it will give me another
In this case, I am trying to create a new column that given a determined condition, (if the id is in a list) the it will give me a 'male' string as a value in the new column, but if the id is not in the list it will give me a female, this is what I am trying:
def select_user_type(df, male_list):
types = {
1: 'female',
2: 'male'
}
search_column = 'user_id'
df['user_sex'] = df.apply(lambda x: types[2] if x[search_column] in male_list else types[1], axis=1)
return df
The error I get is the following
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Example:
male_list = [354, 899] #Example
output:
name user_id user_sex
Jane 890 female
Jean 899 male
Rita 708 female
John 354 male
Seems to work for me without issues. However lets try;
def select_user_type(df, male_list):
types = {
1: 'female',
2: 'male'
}
search_column = 'user_id'
df = df.assign(user_sex=df.apply(lambda x: types[2] if x[search_column] in male_list else types[1], axis=1))
return df