Execute a function in line pairs in a Pandas dataframe

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?

+5
source share
1 answer

You can group by adjacency at a time (without mutating a DataFrame):

 In [11]: g = df.groupby(df.index // 2) 

and then do whatever you need:

 In [12]: g.get_group(0) Out[12]: Name 0 A 1 B In [13]: g.sum() Out[13]: Name 0 AB 1 CD 2 E 
+4
source

Source: https://habr.com/ru/post/1236484/


All Articles