简体   繁体   中英

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

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!

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM