Python数据帧计算元素列表的最小值和最大值

大陆

我有一个包含 2000 行、两列的大数据框,每列行由一个包含大约 1000 个点的列表组成。我想同时删除两列中的负值,然后计算最小值和最大值。目前我正在通过for循环进行,需要 30 分钟才能完成。我可以通过向量化操作来做同样的操作吗?

预期的解决方法:

df = pd.DataFrame({'x':[[-1,0,1,2,10],[1.5,2,4,5]],'y':[[2.5,2.4,2.3,1.5,0.1],[5,4.5,3,-0.1]]})
df = 
                   x                          y
0  [-1, 0, 1, 2, 10]  [2.5, 2.4, 2.3, 1.5, 0.1] 
1     [1.5, 2, 4, 5]          [5, 4.5, 3, -0.1]
### x, y are paired data coming from field. Ex, (-1,2.5), (0,2.4)
# First step: drop negative values in both x and y columns. 
# Find a negative x or y and drop the pair.
# Ex, in first row, drop (-1,2.5) pair. That is, -1 in x and 2.5 in y.
# After dropping negative values
df = 
         x                          y
0  [0, 1, 2, 10]  [2.4, 2.3, 1.5, 0.1] 
1     [1.5, 2, 4]          [5, 4.5, 3]

### Setp2: Find Max in each column
df = 
           x                  y               xmax    ymax
0     [0, 1, 2, 10]  [2.4, 2.3, 1.5, 0.1]      10      2.4
1     [1.5, 2, 4]    [5, 4.5, 3]               4       5

### Setp3: Find y@xmax, x@ymax in each column
df = 
           x                  y               xmax    ymax   y@xmax   x@ymax
0     [0, 1, 2, 10]  [2.4, 2.3, 1.5, 0.1]      10      2.4     0.1      0
1     [1.5, 2, 4]    [5, 4.5, 3]               4       5       3        1.5

目前的解决方案:它正在工作,但需要大量时间。

for i in range(len(df)):
   ### create an auxiliary dataframe
   auxdf = pd.DataFrame({'x':df['x'].loc[i],'y':df['y'].loc[i]})
   ## Step1: drop negative values
   auxdf = auxdf[(auxdf['x']>0)&(auxdf['y']>0)]
   ### Step2: Max in x and y
   xmax = auxdf['x'].max()
   ymax = auxdf['y'].max()
   ### Step3: x@ymax, y@xmax
   xatymax = auxdf['x'].loc[auxdf['y'].idxmax()]
   yatxmax = auxdf['y'].loc[auxdf['x'].idxmax()]
   ### finally I append xmax,ymax,xatymax,yatxmax to the df

做这个向量化操作会最小化时间吗?

舒巴姆·夏尔马

解决方案 numpy

def fast():
    for v in df[['x', 'y']].to_numpy():
        a = np.array([*v])
        a = a[:, (a >= 0).all(axis=0)]
        i = a.argmax(1)
        yield (*a[[0, 1], i], *a[[1, 0], i])


df[['xmax', 'ymax', 'y@xmax', 'x@ymax']] = list(fast())

结果

print(df)

                   x                          y  xmax  ymax  y@xmax  x@ymax
0  [-1, 0, 1, 2, 10]  [2.5, 2.4, 2.3, 1.5, 0.1]  10.0   2.4     0.1     0.0
1     [1.5, 2, 4, 5]          [5, 4.5, 3, -0.1]   4.0   5.0     3.0     1.5

表现

在带有20000行的示例数据帧上

df = pd.concat([df] * 20000, ignore_index=True)

%%timeit
_ = list(fast())
# 1.10 s ± 112 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

计算数据帧的最小值和最大值时出现类型错误

Python中的列表推导,可计算列表的最小值和最大值

从元素列表中找到最大值/最小值

在Java中计算最小值和最大值?

从特定列Scala Spark数据帧获取最小值和最大值

根据最小值和最大值过滤数据帧

如何从包含以下因素的数据帧子集中获取最小值和最大值

在数据帧上查找最大值和最小值,忽略 NA

在 Pandas 数据帧上重复的标签的最小值和最大值

通过嵌套列表中元素的索引找到嵌套列表的最小值和最大值

计算 JavaScript 数组中最小值和最大值之间的元素总和

列表集合中的最小值和最大值

交换列表中的最大值和最小值

在列表中获取最小值和最大值

元组列表中的最小值和最大值

查找列表的最小值和最大值

显示数据库中最大值和最小值之间的下拉列表值

使用 apply 计算向量列表的最大值和最小值

熊猫数据框的最大值和最小值

计算数据框中列的最大值、平均值和最小值

将-infinty值和无穷大值替换为数据帧的最大值和最小值

在Python中查找列表的最小值,最大值

从 Python 中的列表列表中获取最小值和最大值的最快方法?

最小值和最大值

使用Python的字典的最大值和最小值

通过最小值和最大值快速过滤Python数组中的元素

计算非收费算法,以找到大数据帧中变量wrt的最小值和最大值的一个因素?

在Python的列表模式中查找最小值和最大值

Python:比较两个列表,并用符号获取最大值和最小值