I have a table like this
timestamp avg_hr hr_quality avg_rr rr_quality activity sleep_summary_id
1422404668 66 229 0 0 13 78
1422404670 64 223 0 0 20 78
1422404672 64 216 0 0 11 78
1422404674 66 198 0 40 9 78
1422404676 65 184 0 30 3 78
1422404678 64 173 0 10 17 78
1422404680 66 199 0 20 118 78
I'm trying to group the data by timestamp
,sleep id
and rr_quality
, where rr_quality
is > 0
I've tried the following and none of them seems to work
df3 = df2.groupby([df2.index.hour,'sleep_summary_id',df2['rr_quality']>0])
df3 = df2.groupby([df2.index.hour,'sleep_summary_id','rr_quality'>0])
df3 = df2.groupby([df2.index.hour,'sleep_summary_id',['rr_quality']>0])
All of them returns a keyerror.
EDIT:
Also can't seem to be able to pass more than one filter at a time. I tried the following:
df2[df2['rr_quality'] >= 150, df2['hr_quality'] > 200]
df2[df2['rr_quality'] >= 150, ['hr_quality'] > 200]
df2[[df2['rr_quality'] >= 150, ['hr_quality'] > 200]]
returns: TypeError: 'Series' objects are mutable, thus they cannot be hashed
I know this is old but I wanted to add that there is an official function to do exactly this. Transforming the example from pandas to your case:
the simplest thing to do here is to filter the df first and then perform the groupby:
EDIT
If you're intending to assign this back to your original df:
The
loc
call will mask the lhs so that the result of the transform aligns correctlyTo filter using multiple conditions you need to use the array comparision operators
&
,|
and~
forand
,or
andnot
respectively, additionally you need to wrap the conditions in parentheses due to operator precedence: