I have an array x
and one of the values is NaN
.
x = [1, 2, NaN, 3, 5]
All the elements are integer excepting NaN
.
This array type is regarded as float64 not int.
I would like to convert type from floating to string.
I tried astype(str)
. But this make decimal point such as x = [1.0, 2.0, NaN, 3.0, 5.0]
I tried astype(int).astype(str)
. However, in this case, it doesn't work due to NaN element.
So, how I can convert integer array with some of element are NaN to string type without decimal point?
Thank you for reading.
import numpy as np
import pandas as pd
df = pd.DataFrame({'x' : [1,2, np.nan ,3,5]})
# df.dtypes
df['x'] = df['x'].astype(str)
# In this case, it make decimal point.
df['x'] = df['x'].astype(int).astype(str)
# It doesn't work due to NaN element.