i have a df which I want to add a column that shows the student who is place (1) from the group(‘subject’, ‘class’) and update the column after there is a new place (1).
code:
JavaScript
x
10
10
1
data = {
2
"subject": ['eng','math','math','math','math','math','math','math','math','math','math','math','math','eng','eng'],
3
"class": ['Class_4','Class_3','Class_3','Class_3','Class_3','Class_3','Class_3','Class_3','Class_3',
4
'Class_3','Class_3','Class_3','Class_3','Class_4','Class_4'],
5
"student": ['henry','pan','larry','larry','henry','larry','terry','henry','henry',
6
'henry','pan','pan','moose','pan','moose'],
7
"place": [7,8,10,1,7,10,9,7,11,1,11,3,6,2,4]}
8
9
df = pd.DataFrame(data)
10
╔═════════╦═════════╦═════════╦═══════╗ ║ subject ║ class ║ student ║ place ║ ╠═════════╬═════════╬═════════╬═══════╣ ║ eng ║ Class_4 ║ henry ║ 7 ║ ║ math ║ Class_3 ║ pan ║ 8 ║ ║ math ║ Class_3 ║ larry ║ 10 ║ ║ math ║ Class_3 ║ larry ║ 1 ║ ║ math ║ Class_3 ║ henry ║ 7 ║ ║ math ║ Class_3 ║ larry ║ 10 ║ ║ math ║ Class_3 ║ terry ║ 9 ║ ║ math ║ Class_3 ║ henry ║ 7 ║ ║ math ║ Class_3 ║ henry ║ 11 ║ ║ math ║ Class_3 ║ henry ║ 1 ║ ║ math ║ Class_3 ║ pan ║ 11 ║ ║ math ║ Class_3 ║ pan ║ 3 ║ ║ math ║ Class_3 ║ moose ║ 6 ║ ║ eng ║ Class_4 ║ pan ║ 2 ║ ║ eng ║ Class_4 ║ moose ║ 4 ║ ╚═════════╩═════════╩═════════╩═══════╝
result trying to get
╔═════════╦═════════╦═════════╦═══════╦═════════╗ ║ subject ║ class ║ student ║ place ║ new_col ║ ╠═════════╬═════════╬═════════╬═══════╬═════════╣ ║ eng ║ Class_4 ║ henry ║ 7 ║ nil ║ ║ math ║ Class_3 ║ pan ║ 8 ║ nil ║ ║ math ║ Class_3 ║ larry ║ 10 ║ nil ║ ║ math ║ Class_3 ║ larry ║ 1 ║ nil ║ ║ math ║ Class_3 ║ henry ║ 7 ║ larry ║ ║ math ║ Class_3 ║ larry ║ 10 ║ larry ║ ║ math ║ Class_3 ║ terry ║ 9 ║ larry ║ ║ math ║ Class_3 ║ henry ║ 7 ║ larry ║ ║ math ║ Class_3 ║ henry ║ 11 ║ larry ║ ║ math ║ Class_3 ║ henry ║ 1 ║ larry ║ ║ math ║ Class_3 ║ pan ║ 11 ║ henry ║ ║ math ║ Class_3 ║ pan ║ 3 ║ henry ║ ║ math ║ Class_3 ║ moose ║ 6 ║ henry ║ ║ eng ║ Class_4 ║ pan ║ 2 ║ nil ║ ║ eng ║ Class_4 ║ moose ║ 4 ║ nil ║ ╚═════════╩═════════╩═════════╩═══════╩═════════╝
kindly advise. thank you
Advertisement
Answer
Mask the values in place
column which are not equal to 1
then group
the masked column by subject
and class
and use ffill
to forward fill the values
JavaScript
1
3
1
df['new_col'] = df['student'].mask(df['place'] != 1)
2
df['new_col'] = df.groupby(['subject', 'class'])['new_col'].ffill()
3
JavaScript
1
19
19
1
print(df)
2
3
subject class student place new_col
4
0 eng Class_4 henry 7 NaN
5
1 math Class_3 pan 8 NaN
6
2 math Class_3 larry 10 NaN
7
3 math Class_3 larry 1 larry
8
4 math Class_3 henry 7 larry
9
5 math Class_3 larry 10 larry
10
6 math Class_3 terry 9 larry
11
7 math Class_3 henry 7 larry
12
8 math Class_3 henry 11 larry
13
9 math Class_3 henry 1 henry
14
10 math Class_3 pan 11 henry
15
11 math Class_3 pan 3 henry
16
12 math Class_3 moose 6 henry
17
13 eng Class_4 pan 2 NaN
18
14 eng Class_4 moose 4 NaN
19