Consider the following pd.DataFrame
import numpy as np
import pandas as pd
start_end = pd.DataFrame([[(0, 3), (4, 5), (6, 12)], [(7, 10), (11, 90), (91, 99)]])
values = np.random.rand(1, 99)
The start_end is a pd.DataFrame of shape (X, Y) where each value inside is a tuple of (start_location, end_location) in the values vector. Another way of saying that the values in a particular cell is a vector of different lengths.
Question
If I want to find the mean (for example) of the vector values for each of the cells in the pd.DataFrame, how can I do this in a cost effective way?
I managed to achieve this with an .apply function, but it's quite slow.
I guess I need to find some way to present it in a numpy array and then map it back to the 2d data-frame, but I can't figure out how.
Notes
- The distance between start end can varies and outliers can exist.
- The cell start / end is always non-overlapping with the other cells (it will be interest to see if this prerequisite affect speed of solution).
The generalized problem
More generally speaking I this as a recurring problem of how to make a 3d array, where one of the dimensions is not of equal length to a 2d matrix via some transformation function (mean, min, etc.)