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
JavaScript
x
19
19
1
# DataFrame
2
userID (Int) | startTS (Int) | endTS (Int) | placeID (String)
3
4
# Group Data into Subgroups, one for each User.
5
stayGroup = stayData.groupby('userID')
6
7
for userID, data in stayGroup:
8
9
for index, row in data.iterrows():
10
11
# Stays starting during this Stay.
12
staysA = data[row['startTS'] < data['startTS'] < row['endTS']]
13
14
# Stays ending during this Stay.
15
staysB = data[row['startTS'] < data['endTS'] < row['endTS']]
16
17
# Stays starting before and ending after this Stay.
18
staysC = data[(row['startTS'] >= data['startTS']) & (row['endTS'] <= data['endTS'])]
19
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
JavaScript
1
4
1
data['startTS'].between(row['startTS'], row['endTS'], inclusive='neither')
2
# or
3
(row['startTS'] < data['startTS']) & (df['startTS'] < row['endTS'])
4