Python 熊猫:处理具有多种数据类型的列
我的dataframe df中有一列,其中包含float和str类型的值:Python 熊猫:处理具有多种数据类型的列,python,pandas,dataframe,data-analysis,Python,Pandas,Dataframe,Data Analysis,我的dataframe df中有一列,其中包含float和str类型的值: df['ESTACD'].unique() Output: array([11.0, 32.0, 31.0, 35.0, 37.0, 84.0, 83.0, 81.0, 97.0, 39.0, 38.0, 40.0, 34.0, 7.0, 17.0, 16.0, 14.0, 82.0, 8.0, '11', '40', '31', '39', '68', '97', '32', '33', '37', '3
df['ESTACD'].unique()
Output:
array([11.0, 32.0, 31.0, 35.0, 37.0, 84.0, 83.0, 81.0, 97.0, 39.0,
38.0, 40.0, 34.0, 7.0, 17.0, 16.0, 14.0, 82.0, 8.0, '11', '40',
'31', '39', '68', '97', '32', '33', '37', '38', '83', '84', '93',
'35', '81', '67', '07', '80', '71', 'A3', '14', '17', '22', '34',
'36', '82', '08'], dtype=object)
我希望将此列的所有值转换为字符串类型。在这里使用astype(str)
是不够的,因为我们最终会得到像“11.0”、“32.0”这样的值
我能想到的唯一其他方法是使用for循环:
for i in range(len(df)):
if (type(df['ESTACD'][i]) == float) or (df['ESTACD'][i].startswith('0')):
df['ESTACD'][i] = str(int(df['ESTACD'][i]))
但是,这在大型数据集上非常耗时。有没有一种不用循环就能实现的方法?我认为这里应该非常快速地使用纯python,比如:
df['ESTACD'] = [str(int(x)) if isinstance(x, float) else x for x in df['ESTACD']]
像
'08'
这样的值应该变成什么<代码>8或保持为08
?它们必须转换为8,这是or的第二部分所做的。