Let's say I have the following data framework:
>>> df=pd.DataFrame(data=['A','B','C','D','E'], columns=['Name']) >>> df Name 0 A 1 B 2 C 3 D 4 E >>>
I want to create a list of values ββfor adjacent rows in a data frame. If I create an index of pairs, I can get this result using groupby:
>>> df.index=[0,0,1,1,2] >>> df.groupby(level=0).agg(lambda x: list(x)) Name 0 [A, B] 1 [C, D] 2 [E]
What is the most efficient way to do this?
source share