I have an excel file that can calculate the last column with a formula. I am trying to replicate this into python code. How can I perform an operation to achieve such results?
Essentially, the last column is supposed to take, for example, row 2 and subtract itself by row 0 (because they share the same industry besides the month, which I am trying to compare the two by).
I tried using a for loop and take the index of row i
then subtract row i
‘s ‘No. of jobs’ by the row i-2
‘s ‘No. of jobs’ and adding that result to the last column. Then, I would increment i
by 1 to let the operation continue for the next row, but I was unsuccessful.
JavaScript
x
12
12
1
Period Industry No. of jobs Difference from prev. month
2
0 January Farm 70200 N/A
3
1 January Mining 4900 N/A
4
2 February Farm 70100 -100
5
3 February Mining 4850 -50
6
4 March Farm 70200 100
7
5 March Mining 4600 -250
8
6 April Farm 70300 100
9
7 April Mining 5200 600
10
8 May Farm 70300 0
11
9 May Mining 5300 100
12
Advertisement
Answer
Try as follows:
JavaScript
1
16
16
1
df['Difference from prev. month'] = df['No. of jobs'].groupby(df['Industry']).diff()
2
3
df['Difference from prev. month']
4
5
0 NaN
6
1 NaN
7
2 -100.0
8
3 -50.0
9
4 100.0
10
5 -250.0
11
6 100.0
12
7 600.0
13
8 0.0
14
9 100.0
15
Name: Difference from prev. month, dtype: float64
16