Reputation: 55
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?
Upvotes: 0
Views: 2227
Reputation: 71620
Use the below code:
import pandas as pd
import random
n = 0
df = pd.DataFrame(columns=['A', 'B', 'New Column'])
while n < 10:
df = df.append({'A': random.uniform(0, 1), 'B': random.uniform(0, 1), 'New Column': n},
ignore_index=True)
df = df.append({'A': random.uniform(0, 1), 'B': random.uniform(0, 1), 'New Column': n},
ignore_index=True)
print('This value should be added to the new rows only ' + str(n))
n += 1
print(df)
Simply just have a New Column
from the beginning, and then add the value just like you do with A
and B
inside the append
, I am making New Column
also in the same append
s.
Your code doesn't work since you're modifying all values on every loop occasion, when you need it for the appended dataframe, which is what I'm doing.
Upvotes: 1