小编tk.*_*tk.的帖子

为什么Pandas中的Pandas .loc速度取决于DataFrame的初始化?如何尽可能快地制作MultiIndex .loc?

我正在努力提高代码性能.我使用Pandas 0.19.2和Python 3.5.

我只是意识到,根据数据帧初始化,一次写入一大堆值的.loc具有非常不同的速度.

有人可以解释原因,并告诉我什么是最好的初始化?它可以让我加快我的代码.

这是一个玩具的例子.我创建了"相似"的数据帧.

import pandas as pd
import numpy as np


ncols = 1000
nlines = 1000

columns = pd.MultiIndex.from_product([[0], [0], np.arange(ncols)])
lines = pd.MultiIndex.from_product([[0], [0], np.arange(nlines)])

#df has multiindex
df = pd.DataFrame(columns = columns, index = lines)

#df2 has mono-index, and is initialized a certain way
df2 = pd.DataFrame(columns = np.arange(ncols), index = np.arange(nlines))
for i in range(ncols):
    df2[i] = i*np.arange(nlines)

#df3 is mono-index and not initialized
df3 = pd.DataFrame(columns = np.arange(ncols), index = np.arange(nlines))

#df4 …
Run Code Online (Sandbox Code Playgroud)

python performance pandas

5
推荐指数
1
解决办法
897
查看次数

标签 统计

pandas ×1

performance ×1

python ×1