I want the difference of column 'position' by the column 'Seg' with 'x' adjacent.
import numpy as np
import pandas as pd
mydict = {'position':['0.0', '0.433', '2.013', '3.593', '5.173', '6.753', '6.9'],'Seg':['x', 'x', np.nan, np.nan, np.nan, np.nan, 'x']}
df = pd.DataFrame.from_dict(mydict)
df
position Seg
0 0.0 x
1 0.433 x
2 2.013 NaN
3 3.593 NaN
4 5.173 NaN
5 6.753 NaN
6 6.9 x
How can I get the difference 'diff' and 'Seg ID'? Note: 'x' can randomly be at any rows and 'Seg ID' changes accordingly.
position Seg diff Seg ID
0 0.0 x NaN NaN
1 0.433 x 0.433 Seg 1
2 2.013 NaN NaN NaN
3 3.593 NaN NaN NaN
4 5.173 NaN NaN NaN
5 6.753 NaN NaN NaN
6 6.9 x 6.467 Seg 2
Convert strings in
positionto float bySeries.astypefirst:Get differencies by
Series.diffby mask - compare byx:Create counter if difference is not
NaNbySeries.cumsum:Different input data: