So i got an Pandas DataFrame that looks like this:
import pandas as pd
df1 = pd.DataFrame([[5618, 5863, 8873, 7903, 9477, 7177, 7648, 9592],
[5698, 6009, 8242, 7356, 6191, 8817, 7340, 11781],
[5721, 6858, 8401, 6826, 6910, 6243, 6814, 9704]],
columns=["Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb"])
Output:
Jul Aug Sep Oct Nov Dec Jan Feb
0 5618 5863 8873 7903 9477 7177 7648 9592
1 5698 6009 8242 7356 6191 8817 7340 11781
2 5721 6858 8401 6826 6910 6243 6814 9704
At first i want to insert 2 new rows with index 3 and 4: In the first one i want to divide the values of row 1 by the values of row 0:
Jul Aug Sep Oct Nov Dec Jan Feb
3 101,42 102,49 92,88 93,07 65,32 122,8 95,97 122,82
In the second one i want to divide the values of row 1 by the values of row 2:
Jul Aug Sep Oct Nov Dec Jan Feb
4 99,59 87,62 98,10 107,76 89,59 141,23 107,71 121,40
In the next step i want to create a new column with the sum of the raw-values of the month and the averages of the new created rows.
df1["Sum_Avg"] = df1.sum(axis=1)
Output:
Jul Aug Sep Oct Nov Dec Jan Feb Sum_Avg
0 5618 5863 8873 7903 9477 7177 7648 9592 62151
1 5698 6009 8242 7356 6191 8817 7340 11781 61434
2 5721 6858 8401 6826 6910 6243 6814 9704 57477
I don't know how to create the rows with index 3 and 4, so i even don't know how to put the averages in the same row as the sums.
At the end the full table should look like this: Img
What i tried so far:
Making a new DataFrame with the Row 0:
df2 = pd.DataFrame(df1.iloc[[0]])
df2
Output:
Jul Aug Sep Oct Nov Dec Jan Feb
0 5618 5863 8873 7903 9477 7177 7648 9592
Making a new DataFrame with Row 1:
df3 = pd.DataFrame(df1.iloc[[1]])
df3
Output:
Jul Aug Sep Oct Nov Dec Jan Feb
1 5698 6009 8242 7356 6191 8817 7340 11781
Making a new DataFrame with the division of df2 and df3:
df4 = df3/df2
df4
Output:
Jul Aug Sep Oct Nov Dec Jan Feb
0 NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN
and here things messed up and this is the reason why i'm creating this post.