- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
自己试试看:
import pandas as pd
s=pd.Series(xrange(5000000))
%timeit s.loc[[0]] # You need pandas 0.15.1 or newer for it to be that slow
1 loops, best of 3: 445 ms per loop
更新:大概是2014年8月左右在0.15.1中引入的a legitimate bug in pandas。解决方法:使用旧版本的 pandas 等待新版本发布;得到一个尖端的开发者。来自github的版本;在您发布的 pandas
中手动进行一行修改;暂时使用 .ix
而不是 .loc
。
我有一个包含 480 万行的 DataFrame,使用 .iloc[[ id ]]
(带有单元素列表)选择单行需要 489 毫秒,将近半秒,比相同的方法慢 1,800 倍.ix[[ id ]]
,并且比 .iloc[id]
慢 3,500 倍(将 id 作为值而不是列表传递)。公平地说,无论列表的长度如何,.loc[list]
花费的时间都差不多,但我不想在上面花费 489 毫秒,尤其是当 .ix
快一千倍,并且产生相同的结果时结果。我的理解是 .ix
应该更慢,不是吗?
我正在使用 Pandas 0.15.1。关于 Indexing and Selecting Data 的优秀教程表明 .ix
在某种程度上比 .loc
和 .iloc
更通用,而且可能更慢。具体来说,它说
However, when an axis is integer based, ONLY label based access and not positional access is supported. Thus, in such cases, it’s usually better to be explicit and use .iloc or .loc.
这是一个带有基准测试的 iPython session :
print 'The dataframe has %d entries, indexed by integers that are less than %d' % (len(df), max(df.index)+1)
print 'df.index begins with ', df.index[:20]
print 'The index is sorted:', df.index.tolist()==sorted(df.index.tolist())
# First extract one element directly. Expected result, no issues here.
id=5965356
print 'Extract one element with id %d' % id
%timeit df.loc[id]
%timeit df.ix[id]
print hash(str(df.loc[id])) == hash(str(df.ix[id])) # check we get the same result
# Now extract this one element as a list.
%timeit df.loc[[id]] # SO SLOW. 489 ms vs 270 microseconds for .ix, or 139 microseconds for .loc[id]
%timeit df.ix[[id]]
print hash(str(df.loc[[id]])) == hash(str(df.ix[[id]])) # this one should be True
# Let's double-check that in this case .ix is the same as .loc, not .iloc,
# as this would explain the difference.
try:
print hash(str(df.iloc[[id]])) == hash(str(df.ix[[id]]))
except:
print 'Indeed, %d is not even a valid iloc[] value, as there are only %d rows' % (id, len(df))
# Finally, for the sake of completeness, let's take a look at iloc
%timeit df.iloc[3456789] # this is still 100+ times faster than the next version
%timeit df.iloc[[3456789]]
输出:
The dataframe has 4826616 entries, indexed by integers that are less than 6177817
df.index begins with Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], dtype='int64')
The index is sorted: True
Extract one element with id 5965356
10000 loops, best of 3: 139 µs per loop
10000 loops, best of 3: 141 µs per loop
True
1 loops, best of 3: 489 ms per loop
1000 loops, best of 3: 270 µs per loop
True
Indeed, 5965356 is not even a valid iloc[] value, as there are only 4826616 rows
10000 loops, best of 3: 98.9 µs per loop
100 loops, best of 3: 12 ms per loop
最佳答案
Pandas 索引非常慢,我切换到 numpy 索引
df=pd.DataFrame(some_content)
# takes forever!!
for iPer in np.arange(-df.shape[0],0,1):
x = df.iloc[iPer,:].values
y = df.iloc[-1,:].values
# fast!
vals = np.matrix(df.values)
for iPer in np.arange(-vals.shape[0],0,1):
x = vals[iPer,:]
y = vals[-1,:]
关于python - 为什么 DataFrame.loc[[1]] 比 df.ix [[1]] 慢 1,800 倍,比 df.loc[1] 慢 3,500 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596832/
努力理解标题中 5 个示例之间的区别。系列与数据框有一些用例吗?什么时候应该使用一个而不是另一个?哪些是等价的? 最佳答案 df[x] — 使用变量 x 索引列。返回 pd.Series df[[x]
在使用Jupyter Notebook时,我必须为问题标题中提到的df.info()、df.head()等单独留出空格. 有没有办法像第二张图片那样把所有这些都放在一个 block 中,并显示所有信息
我想求三列之和,我采取的方法如下: In [14]: a_pd = pd.DataFrame({'a': np.arange(3), 'b': [5, 7,
我想我们大多数人已经使用过这样的东西(至少如果你正在使用 tidyverse): library(tidyverse) example % select(- mpg) 我的问题: 我知道这部分有一
我有一个 DF,里面有大约 20,000 行。我构建了一个 Python 脚本来对这些数据(包括数据透视表)运行大量清理和数学运算。 我想将此 DF 拆分为 3 个独立的 DF,然后根据列值将这 3
我什至不知道如何表达这一点,但在 Python 中有没有一种方法可以引用等号之前的文本,而无需实际再次编写? ** 编辑 - 我在 Jupyter 中使用 python3 我似乎用了半辈子的时间来写作
在 df1 中,每个单元格值都是我想要从 df2 中获取的行的索引。 我想获取 df2 trial_ms 列中行的信息,然后根据获取的 df2 列重命名 df1 中的列。 可重现的 DF: # df1
我想转换此表 0 thg John 3.0 1 thg James 4.0 2 mol NaN 5.0 3 mol NaN NaN 4
我有一个数据框,我想从中提取 val 中的值大于 15 以及 val 不是 NA: df[ !is.na(df$val) & df$val > 15, ] 由于我假设在 R 中经常需要这样的比较,所
鉴于 coming deprecation of df.ix[...] 如何替换这段代码中的 .ix? df_1 = df.ix[:, :datetime.time(16, 50)] d
任何我可以帮助我说出 Pandas 中这两个语句之间的区别-python df.where(df['colname'] == value) 和 df[(df['colname'] == value)]
考虑 df Index A B C 0 20161001 0 24.5 1 20161001 3 26.5 2
所以我需要按“fh_status”列对行进行分组,然后对每个组执行“gini”的最小值、平均值和最大值(将有三个)。我想出了这段代码: m = (df2.groupby(['fh_status']).
我尝试计算不同公司/股票的一些 KPI。我的股票信息位于 df 中,具有以下结构 Ticker Open High Low Ad
我有一个看起来像这样的 df: gene ID Probe ID Chromosome Start Stop 1: H3F3A 539154271
nn_idx_df 包含与 xyz_df 的索引匹配的索引值。如何从 xyz_df 中的 H 列获取值并在 nn_idx_df 中创建新列以匹配 output_df 中所示的结果。我可以解决这个问题,
我目前的 DF 看起来像这样 Combinations Count 1 ('IDLY', 'VADA') 3734 6 ('DOSA', 'IDLY')
我看到了几个与此相关的问题,但我发现这些技巧都不起作用。 我正在尝试根据第二个数据帧的值填充数据帧的所有 NaN 值。第一个 df 很大,第二个 df 将充当某种键。 DF1 Par
我有两个数据帧,df1 和 df2。每个数据帧的唯一标识符是“ID”和“Prop_Number”。我需要将 df1 中的 Num1、2 和 3 列复制到 df2、1_Num 中的相应列...但我不确定
我有以下数据框: 注意:日期是索引 city morning afternoon evening midnight date 2014-05-01 Y
我是一名优秀的程序员,十分优秀!