0

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?

Alexis
  • 2,104
  • 2
  • 19
  • 40
  • 2
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Anurag Dabas Jul 23 '21 at 03:03
  • I tried to follow the idea but to be honest the question of the link was a little confusing for me. Anyway thank you. – Alexis Jul 23 '21 at 03:16

1 Answers1

2

The recommendation from pandas is to do the setting like below which gets rid of the warning.

df.loc[df['Sex'].isnull(),'Sex']='male'
df.head()
sharathnatraj
  • 1,614
  • 5
  • 14