AlwaysLearning2147
AlwaysLearning2147

Reputation: 103

How can I set nulls in my pandas dataframe column with a different value

I am trying to set some null values in my dataframe to a different value: 'Non-étiquettés'

page_data_fr['Lookup_FR_tag'].loc[page_data_fr['Lookup_FR_tag'].isnull()] = page_data_fr['Lookup_FR_tag'].loc[page_data_fr['Lookup_FR_tag'].isnull()].apply(lambda x: ['Non-étiquettés'])

However, setting the values using this method above results in this warning:

A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

After reading the documentation and attempting to adjust my code, I try this...

dfc = page_data_fr.copy() 

mask = dfc['Lookup_FR_tag'].loc[page_data_fr['Lookup_FR_tag'].isnull()]

dfc['Lookup_FR_tag'].loc[mask, 'Lookup_FR_tag'] = 'Non-étiquettés'

This still generates another error:

ValueError: Cannot mask with non-boolean array containing NA / NaN values dfc['Lookup_FR_tag'].loc[page_data_fr['Lookup_FR_tag'].isnull()]

I have also tried doing it this way which is still no good:

arrOfNulls = []
counter = 0
for x in dfc['Lookup_FR_tag'].isnull():
   if x == True:
      arrOfNulls.append(counter)
      counter += 1
counter += 1

for x in range(len(arrOfNulls)):
    page_data_fr['Lookup_FR_tag'][arrOfNulls[x]] = ['Non-étiquettés']

Any help would be greatly appreciated, I am not sure what I am doing wrong or if I am close..

Upvotes: 0

Views: 2222

Answers (1)

You can do it like this:

 import numpy as np
 
 page_data_fr['Lookup_FR_tag'].replace(np.nan,"Non-étiquettés")

Upvotes: 1

Related Questions