Scenario: I have a dataframe in which one of the rows is empty. Its value should be a sum of two of the other rows.
Data Input:
ISIC2 2018 2019 2020 2021
0 A0 68 95 98 39
1 B0 95 19 5 98
2 B1
3 B2 58 86 10 90
9 C0 36 74 53 97
Expected output:
ISIC2 2018 2019 2020 2021
0 A0 68 95 98 39
1 B0 95 19 5 98
2 B1 153 105 15 188
3 B2 58 86 10 90
9 C0 36 74 53 97
Here, where row ISIC2 == "B1", it should give the value of B0+B2 for each column.
What I tried: I was trying to do this in a loop with fixed references for the rows, but that does not seem to be a very effective way to do this:
year_list_2 = [2018,2019,2020,2021]
for year_var in year_list_2:
for index1 in step4_1.iterrows():
if step4_1.at[index1, "ISIC2"] == "B1":
step4_1.at[index1, year_var] = step4_1.at[index1 - 1, year_var] + step4_1.at[index1 + 1, year_var]
Question: Is there a simpler way to do this?