Skip to content Skip to sidebar Skip to footer

How I Can Create An Interval Column In Pandas?

I hav a data frame like : name1 name2 mounth A B 01 B C 02 A B 02 A B 04 B C

Solution 1:

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

Post a Comment for "How I Can Create An Interval Column In Pandas?"