mercredi 2 juin 2021

Problem with IF conditional with None variable in dataframe [Python]

I have a problem with the None type into a df and I wanted to take the data if the x is not None, as follows:

x = None
y = 5

if x is not None:
    y = x

When is not None y = 5, but if:

x = 9
y = 5

if x is not None:
    y = x

And y = 9.

But when I do this in a df:

Data = {'x': 100,'y': None}  
df = pd.DataFrame(Data, columns= ['x','y', index=[0])

if df.loc[0, 'y'] is not None:
     df.loc[0, 'x'] = df.loc[0, 'y']

df.loc[0, 'x'] = nan

Why 'nan' if it should be 100?

Aucun commentaire:

Enregistrer un commentaire