Reputation: 165
For the following dataframe:
import pandas as pd
df=pd.DataFrame({'list_A':[3,3,3,3,3,\
2,2,2,2,2,2,2,4,4,4,4,4,4,4,4,4,4,4,4]})
How can 'list_A' be manipulated to give 'list_B'?
Desired output:
list_A | list_B | |
---|---|---|
0 | 3 | 1 |
1 | 3 | 1 |
2 | 3 | 1 |
3 | 3 | 0 |
4 | 2 | 1 |
5 | 2 | 1 |
6 | 2 | 0 |
7 | 2 | 0 |
8 | 4 | 1 |
9 | 4 | 1 |
10 | 4 | 1 |
11 | 4 | 1 |
12 | 4 | 0 |
13 | 4 | 0 |
14 | 4 | 0 |
15 | 4 | 0 |
16 | 4 | 0 |
As you can see, if List_A has the number 3 - then the first 3 values of List_B are '1' and then the value of List_B changes to '0', until List_A changes value again.
Upvotes: 1
Views: 47
Reputation: 30920
GroupBy.cumcount
df['list_B'] = df['list_A'].gt(df.groupby('list_A').cumcount()).astype(int)
print(df)
Output
list_A list_B
0 3 1
1 3 1
2 3 1
3 3 0
4 3 0
5 2 1
6 2 1
7 2 0
8 2 0
9 2 0
10 2 0
11 2 0
12 4 1
13 4 1
14 4 1
15 4 1
16 4 0
17 4 0
18 4 0
19 4 0
20 4 0
21 4 0
22 4 0
23 4 0
EDIT
blocks = df['list_A'].ne(df['list_A'].shift()).cumsum()
df['list_B'] = df['list_A'].gt(df.groupby(blocks).cumcount()).astype(int)
Upvotes: 2