Skip to content
Advertisement

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!

Advertisement

Answer

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'])
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement