Python:如何使用系列中的值过滤Pandas DataFrame?

发布于 2025-02-13 16:28:49 字数 987 浏览 3 评论 0原文

上下文

我目前正在处理一些数据并遇到问题。 我想使用系列中的值过滤熊猫数据框。 但是,这总是会引发以下错误:

valueerror:系列的真实价值是模棱两可的。使用A.Empty,A.Bool(),A.Item(),a.any()或a.all()。

代码

# DataFrame
userID (Int) | startTS (Int) | endTS (Int) | placeID (String)

# Group Data into Subgroups, one for each User.
stayGroup = stayData.groupby('userID')

for userID, data in stayGroup:

    for index, row in data.iterrows():

        # Stays starting during this Stay.
        staysA = data[row['startTS'] < data['startTS'] < row['endTS']]

        # Stays ending during this Stay.
        staysB = data[row['startTS'] < data['endTS'] < row['endTS']]

        # Stays starting before and ending after this Stay.
        staysC = data[(row['startTS'] >= data['startTS']) & (row['endTS'] <= data['endTS'])]

问题

有人知道这个错误是什么意思以及我如何解决? 非常感谢您提前提供的帮助!

Context

I am currently processing some data and encountered a problem.
I would like to filter a Pandas DataFrame using Values from a Series.
However, this always throws the following Error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Code

# DataFrame
userID (Int) | startTS (Int) | endTS (Int) | placeID (String)

# Group Data into Subgroups, one for each User.
stayGroup = stayData.groupby('userID')

for userID, data in stayGroup:

    for index, row in data.iterrows():

        # Stays starting during this Stay.
        staysA = data[row['startTS'] < data['startTS'] < row['endTS']]

        # Stays ending during this Stay.
        staysB = data[row['startTS'] < data['endTS'] < row['endTS']]

        # Stays starting before and ending after this Stay.
        staysC = data[(row['startTS'] >= data['startTS']) & (row['endTS'] <= data['endTS'])]

Question

Does anyone have an idea what's this error means and how I can solve it?
Thanks a lot for your assistance in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2025-02-20 16:28:49

问题是row ['startts']&lt; data ['startts']&lt;行['endts'],您可以使用

data['startTS'].between(row['startTS'], row['endTS'], inclusive='neither')
# or
(row['startTS'] < data['startTS']) & (df['startTS'] < row['endTS'])

Problem is row['startTS'] < data['startTS'] < row['endTS'], you can use

data['startTS'].between(row['startTS'], row['endTS'], inclusive='neither')
# or
(row['startTS'] < data['startTS']) & (df['startTS'] < row['endTS'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文