我对Pandas有点迷惑:我希望能够通过取事件相同的区间的并集来合并一个区间列,但只对分离的注解器这样做。例如,我的输入如下:
annotator event interval_presence
3 birds [0,5]
3 birds [7,9]
3 voices [1,2]
3 traffic [1,7]
5 voices [4,7]
5 voices [5,10]
5 traffic [0,1]
其中“interval_presence”中的每一项都是一个列表。我希望将其作为输出:
annotator event interval_presence
3 birds [[0,5],[7,9]]
3 voices [1,2]
3 traffic [1,7]
5 voices [4,10]
5 traffic [0,1]
我知道我可以使用python库“piso”来合并区间,但是我不知道如何合并不同事件和不同注解器上的区间,你知道如何像这样合并我的区间吗?
2条答案
按热度按时间46scxncf1#
这似乎是
pandas.DataFrame.groupby
和itertools.groupy
的 * 超级组合 * 的一个很好的例子:输出:
wxclj1h52#
使用以下示例数据:
此代码将列表转换为间隔:
这个函数分组,然后对区间应用piso.union:
不过,似乎piso目前只支持左闭或右闭的区间。这可能有更深层次的原因,但也可能只是过时了。例如,创建如下的区间:
将获得
AttributeError
:“str”对象没有“closed”属性。