rohi
rohi

Reputation: 305

Replace <NA> values with empty strings in pandas dataframe

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]

Upvotes: 0

Views: 708

Answers (2)

wwnde
wwnde

Reputation: 26676

Just replace with space

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

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

Upvotes: 0

DYZ
DYZ

Reputation: 57033

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)

Upvotes: 1

Related Questions