I want to be able to subtract a specific number of rows from another specific set of rows multiple times. I assume I would do this with a for loop but I can't figure out how exactly to do this.
For example I would like to subtract the yields from the treatment group from the control within the same field. Every 6 rows I would like to subtract the 4-6th row from the 1-3rd rows over and over again
This is the what I have thus far:
dfArray = df ['Crop_Yield'].to_numpy()
`for i in range(len(dfArray('Crop_Yield') //6):`
Thank you in advance for helping me out with this. Sorry if something like this has been asked before, I tired looking but didn't find anything similar.
Below is an example of the table.
Treatment | Field | Crop_Yield |
---|---|---|
Control | 1 Top | 14 |
Control | 2 Top | 18 |
Control | 3 Top | 16 |
Fert A | 1 Bottom | 12 |
Fert A | 2 Bottom | 17 |
Fert A | 3 Bottom | 15 |
Control | 4 Top | 10 |
Control | 5 Top | 13 |
Control | 6 Top | 15 |
Fert B | 4 Bottom | 17 |
Fert B | 5 Bottom | 13 |
Fert B | 6 Bottom | 18 |
CodePudding user response:
I hope I've understood you correctly:
for i in range(len(df) // 6):
df.loc[i * 6 : i * 6 2, "Crop_Yield"] -= df.loc[i * 6 3 : i * 6 5, "Crop_Yield"].to_numpy()
print(df)
Prints:
Treatment Field Crop_Yield
0 Control 1 Top 2
1 Control 2 Top 1
2 Control 3 Top 1
3 Fert A 1 Bottom 12
4 Fert A 2 Bottom 17
5 Fert A 3 Bottom 15
6 Control 4 Top -7
7 Control 5 Top 0
8 Control 6 Top -3
9 Fert B 4 Bottom 17
10 Fert B 5 Bottom 13
11 Fert B 6 Bottom 18