改善熊猫tolist()的性能

时间:2018-12-24 18:24:06

标签: python pandas

我需要执行以下操作,大约需要1s才能对200列的熊猫数据帧执行操作:

for col in mycols:
    values = [str(_item) if col_raw_type == 'object' else '{:f}'.format(_item) 
     for _item in df[col_name].dropna().tolist() 
     if (_item is not None) and str(_item)]

是否有更优化的方法来做到这一点?似乎tolist操作有点慢?

我在这里想要做的是转换如下内容:

field         field2
'2014-01-01'  1.0000000
'2015-01-01'  nan

变成这样:

values_of_field_1 = ['2014-01-01', '2015-01-01']
values_of_field_2 = [1.00000,]

所以我可以推断出列的类型。例如,我想要的最终产品将是:

type_of_field_1 = DATE # %Y-%m-%d
type_of_field_2 = INTEGER #

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试将Series中的整个DataFrame列转换为特定类型。以这个DataFrame为例:

>>> import pandas as pd
>>> import numpy as np

使用混合类型的列创建一个DataFrame:

>>> df = pd.DataFrame({'a': [1, np.nan, 2, 'a', None, 'b'], 'b': [1, 2, 3, 4, 5, 6], 'c': [np.nan, np.nan, 2, 2, 'a', 'a']})
>>> df
      a  b    c
0     1  1  NaN
1   NaN  2  NaN
2     2  3    2
3     a  4    2
4  None  5    a
5     b  6    a
>>> df.dtypes
a    object
b     int64
c    object
dtype: object
>>> for col in df.select_dtypes('object'):
...     print(col)
...     print('\n'.join('{}: {}'.format(v, type(v)) for v in df[col]))
... 
a
1: <class 'int'>
nan: <class 'float'>
2: <class 'int'>
a: <class 'str'>
None: <class 'NoneType'>
b: <class 'str'>
c
nan: <class 'float'>
nan: <class 'float'>
2: <class 'int'>
2: <class 'int'>
a: <class 'str'>
a: <class 'str'>

使用pd.Series.astypeobject dtypes强制转换为str

>>> for col in df.select_dtypes('object'):
...     df[col] = df[col].astype(str)
...     print(col)
...     print('\n'.join('{}: {}'.format(v, type(v)) for v in df[col]))
... 
a
1: <class 'str'>
nan: <class 'str'>
2: <class 'str'>
a: <class 'str'>
None: <class 'str'>
b: <class 'str'>
c
nan: <class 'str'>
nan: <class 'str'>
2: <class 'str'>
2: <class 'str'>
a: <class 'str'>
a: <class 'str'>

答案 1 :(得分:0)

如果您认为tolist()使代码运行缓慢,则可以删除tolist()。根本不需要使用tolist()。下面的代码将为您提供相同的输出。

for col in mycols:
    values = [str(_item) if col_raw_type == 'object' else '{:f}'.format(_item) 
     for _item in df[col_name].dropna()
     if (_item is not None) and str(_item)]
相关问题