I've got a pandas dataframe where each column represents the y values of a descending line from 1 to 0 and the indexes represent the x values. Now I'm interested in finding the intersection points between these lines and a given constant (linearly interpolated).
Example of the desired output:
>>> df = pd.DataFrame({'y1': [1, .7, .4, .1, 0],
'y2': [1, .9, .7, .3, 0],
'y3': [1, .6, .3, .2, 0],
'y4': [1, .7, .5, .3, 0]}, index=[0, 10, 20, 30, 40])
>>> xs = get_intersection(df, .5)
>>> xs
{'x1': 16.6667, # = scipy.interpolate.interp1d([.7, .4], [10, 20])(.5)
'x2': 25.0, # = interp1d([.7, .3], [20, 30])(.5)
'x3': 13.3332, # = interp1d([.6, .3], [10, 20])(.5)
'x4': 20} # No interpolation required
My data consists of roughly 400 rows and 50.000 columns.
possible solution:
I found this SO answer that finds the intersection points between two lines with the following method:
idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
I think this can be adjusted to work with my dataframes, but I'm not sure how to proceed from here:
>>> idx = np.argwhere(np.diff(np.sign(df - .5), axis=0) != 0)
>>> idx
array([[1, 0],
[1, 2],
[1, 3],
[2, 1],
[2, 3]], dtype=int64)
Since people seem to misunderstand the question, I'm interested in finding these points:
Which can be found by linearly interpolating the two nearest points.
Solution: B. M. gave me a step in the right direction:
def get_intersection(df, c):
dfind = len(df) - df.loc[::-1].apply(np.searchsorted, args=(c,), raw=True)
result = {}
for i, v in enumerate(dfind):
result[df.columns.values[i]] = interp1d([df.iloc[v][i], df.iloc[v - 1][i]], [df.index[v], df.index[v - 1]])(.5)
return result
>>> get_intersection(df, .5)
{'y1': array(16.666666666666668), 'y2': array(25.0), 'y3': array(13.333333333333332), 'y4': array(20.0)}