I am tracking some particles on a flat surface using the TrackPy plugin. This results in a dataframe with positions in x and y and a corresponding frame number, here illustrated by a simple list:
x=[80.1,80.2,80.1,80.2,80.3]
y=[40.1,40.2,40.1,40.2,40.3]
frame = [1,2,3,4,5]
However, due to the experimental setup, one may lose the particle for a single frame resulting in:
x=[80.1,80.2,80.1,80.2,80.3]
y=[40.1,40.2,40.1,40.2,40.3]
frame = [1,2,3,4,6]
Now i would like to extend all list, so that ´frame´ is continues and 'x,y' will repeat the previous value if no frame is present in the original data resulting in something like:
x=[80.1,80.2,80.1,80.2,80,2,80.3]
y=[40.1,40.2,40.1,40.2,40.2,40.3]
frame = [1,2,3,4,5,6]
You can use Pandas, which internally utilizes NumPy arrays:
import pandas as pd
df = pd.DataFrame({'x': x, 'y': y}, index=frame)
df = df.reindex(np.arange(df.index.min(), df.index.max()+1)).ffill()
Result
print(df)
x y
1 80.1 40.1
2 80.2 40.2
3 80.1 40.1
4 80.2 40.2
5 80.2 40.2
6 80.3 40.3
You can then extract your result to lists:
x = df['x'].tolist()
y = df['y'].tolist()
frame = df.index.tolist()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With