I have a dataframe(df):
id CI VaR
0 1 0.600 1000
1 1 0.650 1100
2 1 0.700 1200
3 1 0.750 1300
4 2 0.600 2500
5 2 0.650 2600
6 2 0.700 2700
7 2 0.750 2800
8 3 0.600 1500
9 3 0.650 1600
10 3 0.700 1700
11 3 0.750 1800
i have to create a subset dataframe from this dataframe im doing this;
for col in range(1,4):
df2 = df1.loc[df1["id"]==col]
print(df2)
output:
id CI VaR
0 1 0.600 1000
1 1 0.650 1100
2 1 0.700 1200
3 1 0.750 1300
and
4 2 0.600 2500
5 2 0.650 2600
6 2 0.700 2700
7 2 0.750 2800
and
8 3 0.600 1500
9 3 0.650 1600
10 3 0.700 1700
11 3 0.750 1800
this will give me separate dataframe for id 1,2,3 Now i want to VaR value of all dataframe(1 ,2 and 3) and add all the value in the order they are and append to the dataframe of respective. like:
obj = 0
for col in range(1,4):
df2 = df1.loc[df1["id"]==col]
obj = obj + df1["VaR"] # error is here
print(df2)
But this is not working for me
i need ouput like ;
id CI VaR capital
0 1 0.600 1000 5000
1 1 0.650 1100 5300
2 1 0.700 1200 5600
3 1 0.750 1300 5900
capital value 5000 came from adding 1000 + 2500 + 1500 ( these are all first value of respecti id) capital value 5300 came from adding 1100 + 2600 + 1600 ( these are all second value of respecti id) and so on ... and i need for all the id's like;
4 2 0.600 2500 5000
5 2 0.650 2600 5300
6 2 0.700 2700 5600
7 2 0.750 2800 5900
and
8 3 0.600 1500 5000
9 3 0.650 1600 5300
10 3 0.700 1700 5600
11 3 0.750 1800 5900
Thanks for your time :)