tajihiro
tajihiro

Reputation: 2443

How to convert String to Int with Nan data

I would like to convert string data to int to remove decimal point as Integer. It has float format data in data frame.

If the column does not have nan data, it works. However if nan data is like following code, it does not work.

df = pd.Dataframe([[1, '1.0'], [2, ''], [3, '2.8']],
                  columns=['id', 'number'])
df.loc[:, 'number'] = df['number'].astype(float).astype(int)

How can I do for it?

Upvotes: 2

Views: 2562

Answers (1)

jezrael
jezrael

Reputation: 863791

First use to_numeric for convert non numeric to NaNs.

If use pandas 0.24+ is possible use Nullable integer data type:

#get values before `.`
s = df['number'].str.split('\.').str[0]
df['number'] = pd.to_numeric(s, errors='coerce').astype('Int64')

Or use np.floor:

df['number'] = pd.to_numeric(df['number'], errors='coerce').apply(np.floor).astype('Int64')

Or:

df['number'] = np.floor(pd.to_numeric(df['number'], errors='coerce'))
df['number'] = df['number'].astype('Int64')

Upvotes: 6

Related Questions