Reputation: 355
I hav a data frame like :
name1 name2 mounth
A B 01
B C 02
A B 02
A B 04
B C 03
I want to creat a column that shows interval for each pair of names:
name1 name2 interval
A B [1,2];[4,4]
B C [2,3]
Is it possible to create interval like above column in pandas ?
Upvotes: 0
Views: 3094
Reputation: 323226
It is hard to understand why you need this ...
from itertools import chain
df.groupby(['name1','name2']).mounth.\
apply(lambda x :x.groupby(x.diff().ne(1).cumsum()).\
apply(lambda y: list(chain.from_iterable([y])) if len(y)>1 else
list(chain.from_iterable([y,y])))).\
groupby(level=['name1','name2']).apply(list)
Out[1305]:
name1 name2
A B [[1, 2], [4, 4]]
B C [[2, 3]]
Name: mounth, dtype: object
Upvotes: 4