How can I use pandas agg here to avoid iteration?
# pip install pyranges or conda install -c bioconda pyranges

import pyranges as pr

g1 = pr.from_string(&quot;&quot;&quot;Chromosome Start End
chr1   222 223
chr1   223 224
chr1   233 234
chr1   235 236
chr1   2237 238
chr1  2123 2124
chr2   244 245&quot;&quot;&quot;)

g2 = pr.from_string(&quot;&quot;&quot;Chromosome Start End
chr1    221  223
chr1    230  240
chr2    0 1000&quot;&quot;&quot;)

r = g2.count_overlaps(g1)
r
# +--------------+-----------+-----------+------------------+
# | Chromosome   |     Start |       End |   NumberOverlaps |
# | (category)   |   (int32) |   (int32) |          (int64) |
# |--------------+-----------+-----------+------------------|
# | chr1         |       221 |       223 |                1 |
# | chr1         |       230 |       240 |                2 |
# | chr2         |         0 |      1000 |                1 |
# +--------------+-----------+-----------+------------------+
# Unstranded PyRanges object has 3 rows and 4 columns from 2 chromosomes.
# For printing, the PyRanges was sorted on Chromosome.

r.df
# 0       chr1    221   223               1
# 1       chr1    230   240               2
# 2       chr2      0  1000               1