im working with a large set of data and need a more efficient way of doing the following:
rate = [0.03,0.02,0.01]
d = {'portfolio':['abc','de','xyz'], 'A':[0,1,2],'B':[3,4,5]}
df = pd.DataFrame(data=d)
+---+-----------+---+---+
| | portfolio | A | B |
+---+-----------+---+---+
| 0 | abc | 0 | 3 |
| 1 | de | 1 | 4 |
| 2 | xyz | 2 | 5 |
+---+-----------+---+---+
essentially I have several rates that need to run through each rate scenario. and I need to add the rates to last 2 columns
currently this is the code I have:
import pandas as pd
rate = [0.03,0.02,0.01]
scenario_rate = pd.DataFrame()
for i in rate:
d = {'portfolio':['abc','def','xyz'], 'A':[0,1,2],'B':[3,4,5]}
df = pd.DataFrame(data=d)
y = df
y[y.columns[-2:]] += i
y['rate'] = i
scenario_rate = scenario_rate.append(y, ignore_index = True)
+---+-----------+------+------+------+
| | portfolio | A | B | rate |
+---+-----------+------+------+------+
| 0 | abc | 0.03 | 3.03 | 0.03 |
| 1 | def | 1.03 | 4.03 | 0.03 |
| 2 | xyz | 2.03 | 5.03 | 0.03 |
| 3 | abc | 0.02 | 3.02 | 0.02 |
| 4 | def | 1.02 | 4.02 | 0.02 |
| 5 | xyz | 2.02 | 5.02 | 0.02 |
| 6 | abc | 0.01 | 3.01 | 0.01 |
| 7 | def | 1.01 | 4.01 | 0.01 |
| 8 | xyz | 2.01 | 5.01 | 0.01 |
+---+-----------+------+------+------+
how do I do this more efficiently?.. perhaps without a for loop? thank you
scenario_rate = scenario_rate.append(y, ignore_index = True)don't do this. Instead, use a list, then usepd.concatenateat the end. Although, there is likely a better approach overall