Q:

Python pandas: Append rows of DataFrame and delete the appended rows

import pandas as pd
df = pd.DataFrame({
    'id':[1,2,3,4,5,6,7,8,9,10,11],
    'text': ['abc','zxc','qwe','asf','efe','ert','poi','wer','eer','poy','wqr']})

I have a DataFrame with columns:
id    text
1      abc
2      zxc
3      qwe
4      asf
5      efe
6      ert
7      poi
8      wer
9      eer
10     poy
11     wqr

I have a list L = [1,3,6,10] which contains list of id's.
I am trying to append the text column using a list such that, from my list first taking 1 and 3(first two values in a list) and appending text column in my DataFrame with id = 1 which has id's 2, then deleting rows with id column 2 similarly then taking 3 and 6 and then appending text column where id = 4,5 to id 3 and then delete rows with id = 4 and 5 and iteratively for elements in list (x, x+1)
My final output would look like this:
id   text
1    abczxc         # joining id 1 and 2
3    qweasfefe      # joining id 3,4 and 5
6    ertpoiwereer   # joining id 6,7,8,9
10   poywqr         # joining id 10 and 11

A:

You can use isin with cumsum for Series, which is use for groupby with apply join function:
s = df.id.where(df.id.isin(L)).ffill().astype(int)
df1 = df.groupby(s)['text'].apply(''.join).reset_index()
print (df1)
   id          text
0   1        abczxc
1   3     qweasfefe
2   6  ertpoiwereer
3  10        poywqr

It working because:
s = df.id.where(df.id.isin(L)).ffill().astype(int)
print (s)
0      1
1      1
2      3
3      3
4      3
5      6
6      6
7      6
8      6
9     10
10    10
Name: id, dtype: int32

A:

I changed the values not in list to np.nan and then ffill and groupby. Though @Jezrael's approach is much better. I need to  remember to use cumsum:)
l = [1,3,6,10]
df.id[~df.id.isin(l)] = np.nan
df = df.ffill().groupby('id').sum()

        text
id  
1.0     abczxc
3.0     qweasfefe
6.0     ertpoiwereer
10.0    poywqr

