Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫:处理具有多种数据类型的列_Python_Pandas_Dataframe_Data Analysis - Fatal编程技术网

Python 熊猫:处理具有多种数据类型的列

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

我的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', '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的第二部分所做的。