I'm working with a dataframe inside a while loop that needs to be updated based on the simulation time. Here's an example:
import pandas as pd
import random
n = 0
df = pd.DataFrame(columns=['A', 'B'])
while n < 10:
df = df.append({'A': random.uniform(0, 1), 'B': random.uniform(0, 1)},
ignore_index=True)
df = df.append({'A': random.uniform(0, 1), 'B': random.uniform(0, 1)},
ignore_index=True)
df['New Column'] = n
print('This value should be added to the new rows only ' + str(n))
n += 1
print(df)
This results in the entire new column being updated at once, which is undesired in my case. Here are the first three prints:
This value should be added to the new rows only 0
A B New Column
0 0.242312 0.369978 0
1 0.709112 0.650794 0
This value should be added to the new rows only 1
A B New Column
0 0.242312 0.369978 1 <- should have been 0
1 0.709112 0.650794 1 <- should have been 0
2 0.293787 0.229019 1
3 0.828184 0.917984 1
This value should be added to the new rows only 2
A B New Column
0 0.242312 0.369978 2 <- should have been 0
1 0.709112 0.650794 2 <- should have been 0
2 0.293787 0.229019 2 <- should have been 1
3 0.828184 0.917984 2 <- should have been 1
4 0.644289 0.589050 2
5 0.371361 0.759865 2
Can I do this in one operation or I need to copy to another dataframe and then append that?