Home > Blockchain >  Replace <NA> values with empty strings in pandas dataframe
Replace <NA> values with empty strings in pandas dataframe

Time:11-17

I have created dataframe which looks like this. which is of Nullable integer data type.

arr = pd.array([1, 2, None,4], dtype=pd.Int64Dtype())

output:

[1, 2, <NA>,4]

I want to replace <NA> values with an empty string. I have tried different replace methods nothing worked. is there a way that I can achieve this.

I want my output to look like this.

[1, 2, ,4]

CodePudding user response:

You cannot have a string in an <IntegerArray>. You must convert the array to the object type before the replacement:

arr_as_object = arr.astype(object)
arr_as_object[arr.isna()] = ' '
# array([1, 2, ' ', 4], dtype=object)

CodePudding user response:

Just replace with space

arr = np.where(arr.isna(),' ',arr)

array([1, 2, ' ', 4], dtype=object)
  • Related