I have two dataframes. They look like this:
df_a
Framecount probability
0 0.0 [0.00019486549333333332, 4.883635666666667e-06...
1 1.0 [0.00104359155, 3.9232405e-05, 0.0015722045000...
2 2.0 [0.00048501002666666667, 1.668179e-05, 0.00052...
3 3.0 [4.994969500000001e-05, 4.0931635e-07, 0.00011...
4 4.0 [0.0004808829, 5.389742e-05, 0.002522127933333...
.. ... ...
906 906.0 [1.677140566666667e-05, 1.1745095666666665e-06...
907 907.0 [1.5164155000000002e-05, 7.66629575e-07, 0.000...
908 908.0 [8.1334184e-05, 0.00012675669636333335, 0.0028...
909 909.0 [0.00014893802999999998, 1.0407592500000001e-0...
910 910.0 [4.178489e-05, 2.17477925e-06, 0.02094931, 0.0...
And:
df_b
start stop
0 12.12 12.47
1 13.44 20.82
2 20.88 29.63
3 31.61 33.33
4 33.44 42.21
.. ... ...
228 880.44 887.92
229 888.63 892.07
230 892.13 895.30
231 895.31 900.99
232 907.58 908.35
I want to merge df_a.probability onto df_b when df_a.Framecount is in between df_b.start and df_b.stop. The aggregation statistic for df_a.probability should be mean, but I run into errors because df_a.probability is dtype np array.
I am trying to using this code:
idx = pd.IntervalIndex.from_arrays(df_text['start'], df_text['stop'])
df_text.join(df_vid.groupby(idx.get_indexer_non_unique(df_vid['Framecount']))['probability'].apply(np.mean), how='left')
Line 1 creates the index do determine the grouping. In line 2 I am trying to implement the group by and aggregating all of the values in df_a.probability that fall within a groupby index by mean. I want one array per groupby that is the mean of all arrays in the groupby index. This code gives me this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-271-19c7d58fb664> in <module>
1 idx = pd.IntervalIndex.from_arrays(df_text['start'], df_text['stop'])
2 f = lambda x: np.mean(np.array(x.tolist()), axis=0)
----> 3 df_text.join(df_vid.groupby(idx.get_indexer_non_unique(df_vid['Framecount']))['probability'].apply(np.mean), how='left')
~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed)
5808 group_keys=group_keys,
5809 squeeze=squeeze,
-> 5810 observed=observed,
5811 )
5812
~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, mutated)
407 sort=sort,
408 observed=observed,
--> 409 mutated=self.mutated,
410 )
411
~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/grouper.py in get_grouper(obj, key, axis, level, sort, observed, mutated, validate)
588
589 elif is_in_axis(gpr): # df.groupby('name')
--> 590 if gpr in obj:
591 if validate:
592 obj._check_label_or_level_ambiguity(gpr, axis=axis)
~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __contains__(self, key)
1848 def __contains__(self, key) -> bool_t:
1849 """True if the key is in the info axis"""
-> 1850 return key in self._info_axis
1851
1852 @property
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in __contains__(self, key)
3898 @Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
3899 def __contains__(self, key) -> bool:
-> 3900 hash(key)
3901 try:
3902 return key in self._engine
TypeError: unhashable type: 'numpy.ndarray'
I have tried multiple aggregation specifications, including:
df_text.join(df_vid.groupby(idx.get_indexer_non_unique(df_vid['Framecount']))['probability'].apply(lambda x: np.mean(np.array(x.tolist()), axis=0)), how='left')
or
df_text.join(df_vid.groupby(idx.get_indexer_non_unique(df_vid['Framecount']))['probability'].apply((np.mean), how='left')
or
df_text.join(df_vid.groupby(idx.get_indexer_non_unique(df_vid['Framecount']))['probability'].mean()), how='left')
and I get the same error.
How do I accomplish this?