簡體   English   中英

Python:如何使用系列中的值過濾 Pandas DataFrame?

[英]Python: How to filter a Pandas DataFrame using Values from a Series?

語境

我目前正在處理一些數據並遇到了問題。 我想使用系列中的值過濾 Pandas DataFrame。 但是,這總是會引發以下錯誤:

ValueError:Series 的真值不明確。 使用 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'])]

問題

有誰知道這個錯誤是什么意思以及我該如何解決? 非常感謝您提前提供的幫助!

問題是row['startTS'] < data['startTS'] < row['endTS'] ,你可以使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM