I have column in Pandas full of lists. Each row looks like:
row_1 = [7, 8, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
row_2 = [7, 8, 9, 15, 16, 17, 18, 19]
...
row_n = [27, 28, 29, 30, 31, 32, 33, 34, 35]
And I need to get boundary values and insert them to the tuple, eg
full_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...,35]
and according to the list above find first and last values:
row_tuple_1 = [(7, 8), (14, 23)]
row_tuple_2 = [(7, 9), (15, 19)]
...
row_tuple_n = [(27, 35)]
Is there a way to do that?
Upd1:
Imagine that full_list
is how row_1
should look's like. But it's not.
Another option to write row_1
is:
row_1 = [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 7, 8, np.nan,
np.nan, np.nan, np.nan, np.nan, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, np.nan, ...]
So (start, end)
tuple should be (7, 8)
and (14, 23)
Finally know what you need, we need using groupby
for each row
def func(x):
s=pd.Series(x)
return s.groupby(s.diff().ne(1).cumsum()).agg(['first','last']).values.tolist()
df.L.apply(func)
Out[145]:
0 [[7, 8], [14, 23]]
1 [[7, 9], [15, 19]]
2 [[27, 35]]
Name: L, dtype: object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.