Add a line before the first and after the last position of each group in pandas

I have a dataframe

value    id
100       1
200       1
300       1
500       2
600       2
700       3

I want to group by id and add lines to the 1st line and after the last line of each group, so my framework looks like this: I add a line with a value 0

value    id
0         1
100       1
200       1
300       1
0         1
0         2
500       2
600       2
0         2
0         3
700       3
0         3

Now for each id group, I want to add a sequence column to:

value    id    sequence
0         1    1
100       1    2
200       1    3
300       1    4
0         1    5
0         2    1
500       2    2
600       2    3
0         2    4
0         3    1
700       3    2
0         3    3

The last part is simple, but I'm looking for how to add lines before and after each group?

+4
source share
3 answers

Not so easy:

def f(x):
    x = pd.DataFrame(np.concatenate([np.array([[0, x['id'].iat[0]]]), 
                                     x.values,
                                     np.array([[0, x['id'].iat[0]]])]), columns=x.columns)
    return (x)

df = df.groupby('id').apply(f).reset_index(drop=True)

df['seq'] = df.groupby('id').cumcount() + 1
print (df)
    value  id  seq
0       0   1    1
1     100   1    2
2     200   1    3
3     300   1    4
4       0   1    5
5       0   2    1
6     500   2    2
7     600   2    3
8       0   2    4
9       0   3    1
10    700   3    2
11      0   3    3
+3
source

Try groupbywith apply.

df = df.groupby('id')['value']\
         .apply(lambda x: pd.Series([0] + x.tolist() + [0]))\
         .reset_index().drop('level_1', 1)
df 
    id  value
0    1      0
1    1    100
2    1    200
3    1    300
4    1      0
5    2      0
6    2    500
7    2    600
8    2      0
9    3      0
10   3    700
11   3      0

And now use cumcountfor consistency.

df['sequence'] = df.groupby('id').cumcount() + 1

In [228]: df
Out[228]: 
    id  value  sequence
0    1      0         1
1    1    100         2
2    1    200         3
3    1    300         4
4    1      0         5
5    2      0         1
6    2    500         2
7    2    600         3
8    2      0         4
9    3      0         1
10   3    700         2
11   3      0         3
+3
source
data = [zip([k] * (len(group) + 2), [0] + group.values.tolist() + [0]) 
        for k, group in df.groupby('id')['value']]
df = pd.DataFrame([x for g in data for x in g], columns=['id', 'value'])
df.assign(sequence=df.groupby(['id'])['value'].transform(
    lambda group: range(1, group.count() + 1)))
>>> df
    id  value  sequence
0    1      0         1
1    1    100         2
2    1    200         3
3    1    300         4
4    1      0         5
5    2      0         1
6    2    500         2
7    2    600         3
8    2      0         4
9    3      0         1
10   3    700         2
11   3      0         3
+2

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


All Articles