Reputation: 341
I would like to create a new column "Group". The integer values from column "Step_ID" should be converted in loop from 1 to 4. See the image below.
import pandas as pd
data = {'Step_ID': [1, 1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11]}
df1 = pd.DataFrame(data)
Upvotes: 1
Views: 39
Reputation: 102
import pandas as pd
data = {'Step_ID': [1, 1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11]}
df1 = pd.DataFrame(data)
# Need to subtract/add 1 so that you get 1-4 instead of 0-3
df1['group'] = ((df1['Step_ID'] - 1) % 4) + 1
df1
Step_ID | group | |
---|---|---|
0 | 1 | 1 |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 2 | 2 |
4 | 3 | 3 |
5 | 4 | 4 |
6 | 5 | 1 |
7 | 6 | 2 |
8 | 6 | 2 |
9 | 7 | 3 |
10 | 8 | 4 |
11 | 8 | 4 |
12 | 9 | 1 |
13 | 10 | 2 |
14 | 11 | 3 |
15 | 11 | 3 |
Upvotes: 0
Reputation: 14949
TRY:
df['Group'] = (df.Step_ID % 4).replace(0 ,4)
OUTPUT:
Step_ID Group
0 1 1
1 1 1
2 2 2
3 2 2
4 3 3
5 4 4
6 5 1
7 6 2
8 6 2
9 7 3
10 8 4
11 8 4
12 9 1
13 10 2
14 11 3
15 11 3
Upvotes: 2