I have this dataset (the Titanic dataset):
import pandas as pd
import numpy as np
url = 'https://raw.githubusercontent.com/chrisalbon/simulated_datasets/master/titanic.csv'
df = pd.read_csv(url)
And I want to change for the column 'Sex' all the values 'male' with 'NaN'. This is the code:
df['Sex'] = df['Sex'].replace('male',np.nan)
df.head(3)
Name PClass Age Sex Survived SexCode
0 Allen, Miss Elisabeth Walton 1st 29.0 female 1 1
1 Allison, Miss Helen Loraine 1st 2.0 female 0 1
2 Allison, Mr Hudson Joshua... 1st 30.0 NaN 0 0
And I want to roll back and change the NaN values to 'male'. But I tried this:
df['Sex'][df['Sex'].isnull()]='male'
df
But I receive a message: A value is trying to be set on a copy of a slice from a DataFrame
The change was made, but perhaps my logic is bad. Please, do you suggest a best way to code this?