如何在两个值之间的 DataFrame 中选择行

2024-12-24 08:56:00
admin
原创
82
摘要:问题描述:我正在尝试修改 DataFramedf以仅包含列中的值closing_price介于 99 和 101 之间的行,并尝试使用以下代码执行此操作。但是,我得到了错误ValueError:Series 的真值不明确。请使用 a.empty、a.bool()、a.item()、a.any() 或 a.al...

问题描述:

我正在尝试修改 DataFramedf以仅包含列中的值closing_price介于 99 和 101 之间的行,并尝试使用以下代码执行此操作。

但是,我得到了错误

ValueError:Series 的真值不明确。请使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

我想知道是否有一种不使用循环就能做到这一点的方法。

df = df[99 <= df['closing_price'] <= 101]

解决方案 1:

考虑Series.between:

df = df[df['closing_price'].between(99, 101)]

解决方案 2:

您应该使用()对布尔向量进行分组来消除歧义。

df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)]

解决方案 3:

有一个更好的选择 - 使用query()方法:

In [58]: df = pd.DataFrame({'closing_price': np.random.randint(95, 105, 10)})

In [59]: df
Out[59]:
   closing_price
0            104
1             99
2             98
3             95
4            103
5            101
6            101
7             99
8             95
9             96

In [60]: df.query('99 <= closing_price <= 101')
Out[60]:
   closing_price
1             99
5            101
6            101
7             99

更新:回答评论(编辑以修复小错误)

我喜欢这里的语法,但是在尝试与表达式结合时失败了:

df.query('(mean - 2*sd) <= closing_price <= (mean + 2*sd)')

我的数据都在平均值的 2 个标准差之内,因此我将执行 1 来演示:

In [161]: qry = ("(closing_price.mean() - closing_price.std())" +
     ...:        " <= closing_price <= " +
     ...:        "(closing_price.mean() + closing_price.std())")
     ...:

In [162]: df.query(qry)
Out[162]:
   closing_price
1             99
2             98
5            101
6            101
7             99
9             96

或者

In [163]: mean = df['closing_price'].mean()
     ...: sd = df['closing_price'].std()
     ...: df.query('(@mean - @sd) <= closing_price <= (@mean + @sd)')
     ...:
Out [163]:
   closing_price
1             99
2             98
5            101
6            101
7             99
9             96

解决方案 4:

newdf = df.query('closing_price.mean() <= closing_price <= closing_price.std()')

或者

mean = closing_price.mean()
std = closing_price.std()

newdf = df.query('@mean <= closing_price <= @std')

解决方案 5:

如果必须反复调用(针对不同的边界和),则许多工作会不必要地重复。在这种情况下,对帧/系列进行一次排序然后使用会很有帮助。我测得的速度提高了 25 倍,见下文。pd.Series.between(l,r) l`r`pd.Series.searchsorted()

def between_indices(x, lower, upper, inclusive=True):
    """
    Returns smallest and largest index i for which holds 
    lower <= x[i] <= upper, under the assumption that x is sorted.
    """
    i = x.searchsorted(lower, side="left" if inclusive else "right")
    j = x.searchsorted(upper, side="right" if inclusive else "left")
    return i, j

# Sort x once before repeated calls of between()
x = x.sort_values().reset_index(drop=True)
# x = x.sort_values(ignore_index=True) # for pandas>=1.0
ret1 = between_indices(x, lower=0.1, upper=0.9)
ret2 = between_indices(x, lower=0.2, upper=0.8)
ret3 = ...

基准

测量对 的重复评估(n_reps=100pd.Series.between()以及基于 的方法pd.Series.searchsorted(),针对不同的参数lowerupper。在我的 MacBook Pro 2015 上,使用 Python v3.8.0 和 Pandas v1.0.3,以下代码产生以下输出

# pd.Series.searchsorted()
# 5.87 ms ± 321 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# pd.Series.between(lower, upper)
# 155 ms ± 6.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# Logical expressions: (x>=lower) & (x<=upper)
# 153 ms ± 3.52 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
import numpy as np
import pandas as pd

def between_indices(x, lower, upper, inclusive=True):
    # Assumption: x is sorted.
    i = x.searchsorted(lower, side="left" if inclusive else "right")
    j = x.searchsorted(upper, side="right" if inclusive else "left")
    return i, j

def between_fast(x, lower, upper, inclusive=True):
    """
    Equivalent to pd.Series.between() under the assumption that x is sorted.
    """
    i, j = between_indices(x, lower, upper, inclusive)
    if True:
        return x.iloc[i:j]
    else:
        # Mask creation is slow.
        mask = np.zeros_like(x, dtype=bool)
        mask[i:j] = True
        mask = pd.Series(mask, index=x.index)
        return x[mask]

def between(x, lower, upper, inclusive=True):
    mask = x.between(lower, upper, inclusive=inclusive)
    return x[mask]

def between_expr(x, lower, upper, inclusive=True):
    if inclusive:
        mask = (x>=lower) & (x<=upper)
    else:
        mask = (x>lower) & (x<upper)
    return x[mask]

def benchmark(func, x, lowers, uppers):
    for l,u in zip(lowers, uppers):
        func(x,lower=l,upper=u)

n_samples = 1000
n_reps = 100
x = pd.Series(np.random.randn(n_samples))
# Sort the Series.
# For pandas>=1.0:
# x = x.sort_values(ignore_index=True)
x = x.sort_values().reset_index(drop=True)

# Assert equivalence of different methods.
assert(between_fast(x, 0, 1, True ).equals(between(x, 0, 1, True)))
assert(between_expr(x, 0, 1, True ).equals(between(x, 0, 1, True)))
assert(between_fast(x, 0, 1, False).equals(between(x, 0, 1, False)))
assert(between_expr(x, 0, 1, False).equals(between(x, 0, 1, False)))

# Benchmark repeated evaluations of between().
uppers = np.linspace(0, 3, n_reps)
lowers = -uppers
%timeit benchmark(between_fast, x, lowers, uppers)
%timeit benchmark(between, x, lowers, uppers)
%timeit benchmark(between_expr, x, lowers, uppers)

解决方案 6:

相反

df = df[99 <= df['closing_price'] <= 101]

你应该用这个

df = df[(99 <= df['closing_price']) & (df['closing_price'] <= 101)]

我们必须使用 NumPy 的按位逻辑运算符|, &, ~,^来进行复合查询。此外,括号对于运算符优先级也很重要。

欲了解更多信息,请访问链接:比较、掩码和布尔逻辑(摘自 Jake VanderPlas 撰写的《Python 数据科学手册》)。

解决方案 7:

如果您要处理多个值和多个输入,也可以设置这样的应用函数。在这种情况下,过滤数据框中属于特定范围内的 GPS 位置。

def filter_values(lat,lon):
    if abs(lat - 33.77) < .01 and abs(lon - -118.16) < .01:
        return True
    elif abs(lat - 37.79) < .01 and abs(lon - -122.39) < .01:
        return True
    else:
        return False


df = df[df.apply(lambda x: filter_values(x['lat'],x['lon']),axis=1)]
相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1120  
  IPD(Integrated Product Development,集成产品开发)流程是一种广泛应用于高科技和制造业的产品开发方法论。它通过跨职能团队的紧密协作,将产品开发周期缩短,同时提高产品质量和市场成功率。在IPD流程中,CDCP(Concept Decision Checkpoint,概念决策检查点)是一个关...
IPD培训课程   75  
  研发IPD(集成产品开发)流程作为一种系统化的产品开发方法,已经在许多行业中得到广泛应用。它不仅能够提升产品开发的效率和质量,还能够通过优化流程和资源分配,显著提高客户满意度。客户满意度是企业长期成功的关键因素之一,而IPD流程通过其独特的结构和机制,能够确保产品从概念到市场交付的每个环节都围绕客户需求展开。本文将深入...
IPD流程   66  
  IPD(Integrated Product Development,集成产品开发)流程是一种以跨职能团队协作为核心的产品开发方法,旨在通过优化资源分配、提高沟通效率以及减少返工,从而缩短项目周期并提升产品质量。随着企业对产品上市速度的要求越来越高,IPD流程的应用价值愈发凸显。通过整合产品开发过程中的各个环节,IPD...
IPD项目管理咨询   76  
  跨部门沟通是企业运营中不可或缺的一环,尤其在复杂的产品开发过程中,不同部门之间的协作效率直接影响项目的成败。集成产品开发(IPD)作为一种系统化的项目管理方法,旨在通过优化流程和增强团队协作来提升产品开发的效率和质量。然而,跨部门沟通的复杂性往往成为IPD实施中的一大挑战。部门之间的目标差异、信息不对称以及沟通渠道不畅...
IPD是什么意思   70  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用