在 pandas 中有效地使用替换

Using replace efficiently in pandas

我希望在 python3 中以高效的方式使用 replace 函数。我拥有的代码正在完成任务,但速度太慢,因为我正在处理大型数据集。因此,只要有权衡,我的首要任务就是效率而不是优雅。这是我想做的玩具:

import pandas as pd
df = pd.DataFrame([[1,2],[3,4],[5,6]], columns = ['1st', '2nd'])

       1st  2nd
   0    1    2
   1    3    4
   2    5    6


idxDict= dict()
idxDict[1] = 'a'
idxDict[3] = 'b'
idxDict[5] = 'c'

for k,v in idxDict.items():
    df ['1st'] = df ['1st'].replace(k, v)

给出

     1st  2nd
   0   a    2
   1   b    4
   2   c    6

如我所愿,但时间太长了。最快的方法是什么?

编辑:这是一个比 问题更集中、更清晰的问题,后者的解决方案相似。

使用 map 执行查找:

In [46]:
df['1st'] = df['1st'].map(idxDict)
df
Out[46]:
  1st  2nd
0   a    2
1   b    4
2   c    6

为了避免没有有效密钥的情况你可以传递na_action='ignore'

您也可以使用 df['1st'].replace(idxDict) 但要回答您有关效率的问题:

计时

In [69]:
%timeit df['1st'].replace(idxDict)
%timeit df['1st'].map(idxDict)

1000 loops, best of 3: 1.57 ms per loop
1000 loops, best of 3: 1.08 ms per loop

In [70]:    
%%timeit
for k,v in idxDict.items():
    df ['1st'] = df ['1st'].replace(k, v)

100 loops, best of 3: 3.25 ms per loop

所以在这里使用 map 快了 3 倍多

在更大的数据集上:

In [3]:
df = pd.concat([df]*10000, ignore_index=True)
df.shape

Out[3]:
(30000, 2)

In [4]:    
%timeit df['1st'].replace(idxDict)
%timeit df['1st'].map(idxDict)

100 loops, best of 3: 18 ms per loop
100 loops, best of 3: 4.31 ms per loop

In [5]:    
%%timeit
for k,v in idxDict.items():
    df ['1st'] = df ['1st'].replace(k, v)

100 loops, best of 3: 18.2 ms per loop

对于 30K 行 df,mapreplace 或循环

快约 4 倍,因此它的扩展性更好

map is indeed faster, replace was updated in version 19.2 (details here) 提高其速度使差异显着减小:

In [1]:
import pandas as pd


df = pd.DataFrame([[1,2],[3,4],[5,6]], columns = ['1st', '2nd'])
df = pd.concat([df]*10000, ignore_index=True)
df.shape

Out [1]:
(30000, 2)

In [2]:
idxDict = {1:'a', 3:"b", 5:"c"}
%timeit df['1st'].replace(idxDict, inplace=True)
%timeit df['1st'].update(df['1st'].map(idxDict))

Out [2]:
100 loops, best of 3: 12.8 ms per loop
100 loops, best of 3: 7.95 ms per loop

此外,我修改了 EdChum 的地图代码以包含 update,这虽然速度较慢,但​​可以防止未包含在不完整地图中的值更改为 nans。

如果不需要 NaN 传播——您想替换值但保留字典中不匹配的值——还有两个其他选项:

def numpy_series_replace(series: pd.Series, mapping: dict) -> pd.Series:
    """Replace values in a series according to a mapping."""
    result = series.copy().values
    for k, v in mapping.items():
        result[series.values==k] = v
    return pd.Series(result, index=series.index)

def apply_series_replace(series: pd.Series, mapping: dict) -> pd.Series:
    return series.apply(lambda y: mapping.get(y,y))

numpy 的实现感觉有点老套,但速度更快。

v = pd.Series(np.random.randint(0, 10, 1000000))
mapper = {0: 1, 3: 2}

%timeit numpy_series_replace(v, mapper)
60.1 ms ± 200 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit apply_series_replace(v, mapper)
311 ms ± 10.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)