Python 如何使用Nan数据将字符串转换为Int

Python 如何使用Nan数据将字符串转换为Int,python,pandas,Python,Pandas,我想将字符串数据转换为int,以将小数点作为整数删除。 它在数据帧中具有浮点格式的数据 如果该列没有nan数据,则它可以工作。 但是,如果nan数据类似于下面的代码,则它不起作用 df = pd.Dataframe([[1, '1.0'], [2, ''], [3, '2.8']], columns=['id', 'number']) df.loc[:, 'number'] = df['number'].astype(float).astype(int) 我

我想将字符串数据转换为int,以将小数点作为整数删除。 它在数据帧中具有浮点格式的数据

如果该列没有nan数据,则它可以工作。 但是,如果nan数据类似于下面的代码,则它不起作用

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

首先用于将非数字转换为
NaN
s

如果可以使用熊猫
0.24+
,请使用:

或者使用
np.floor

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

首次用于将非数字转换为
NaN
s

如果可以使用熊猫
0.24+
,请使用:

或者使用
np.floor

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

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