0

I am cleaning my dataset and just in one column (dtype obj) has NaN that I want to convert / transform with the same values given by other variable (obj). Do you know how can I transform those NaN without overwriting the non-NaN values.

Here is an example of I would like to do: in the areas where there is NaN values I want to set the values of region, in this particular case 'NaN' = 'Europe' and 'NaN' = 'Africa'

Region Area
USA NY
Europe Berlin
Asia Beijin
Europe NaN
Africa NaN

I tried using a for loop: but i guess is wrong

Area_type = df['Area']
   def Area_type (x):
    for i in Area_type:
         if i == "NaN":
          i = df['Region']
         else:
             pass
     return Area_type

Thanks a lot

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
Alex
  • 35
  • 3
  • How is the data stored? what is `df`? – Rick M. Sep 02 '22 at 14:19
  • Does this answer your question? [Efficiently replace values from a column to another column Pandas DataFrame](https://stackoverflow.com/questions/39903090/efficiently-replace-values-from-a-column-to-another-column-pandas-dataframe) – Ignatius Reilly Sep 02 '22 at 14:22
  • Hi Rick , yes its strored as df. – Alex Sep 02 '22 at 21:39
  • I tried both method, replace and np.where, but give some errors. I am going to work a little bit more to get familiar with those functions. – Alex Sep 02 '22 at 22:34

1 Answers1

3

You can instruct pandas to change the value of a column in a subset of records that match a condition by using loc:

df.loc[df["Area"].isna(), "Area"] = df["Region"]

If NaN values are strings, use this:

df.loc[df["Area"] == "NaN", "Area"] = df["Region"]
HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93