I am iterating through a dataframe and I am trying to add values to a specific column for each row but when I print the resulting data frame the values are not present
#add two new blank columns to the dayData dataframe
dayData["myValue1"]=""
dayData["myValue2"]=""
#iterate over the dataframe
for idxDay, row in dayData.iterrows():
do something.....
#interate again through the dataframe
for idxRange, row1 in dayData.iterrows():
do something else....
calculate value1
calculate value2
#write the result for value1 and value2 to the dayData dataframe
row["myValue1"]=value1
row["myValue2"]=value2
print(dayData)
The values for value1 and value 2 are correct and even when I hard code value1 = 1 and value2 = 2 when I print dayData the columns after columns myValue1 and myValue2 should be updated they contain no data.
the resulting dayData dataframe should look like
vwap last volume ratio myLong myShort
0 301.071871 301.221525 43133218.0 1.000497 1 2
1 215.545413 213.791400 349730738.0 0.991862 3 3
but instead I just get:
vwap last volume ratio myLong myShort
0 301.071871 301.221525 43133218.0 1.000497
1 215.545413 213.791400 349730738.0 0.991862
Instead of assigning to the row, assign back to the dataframe with index. The row is a copy of the row and changes to it won't persist in the parent dataframe.
Instead of:
row["myValue1"]=value1
row["myValue2"]=value2
Do:
dayData.loc[idxDay, "myValue1"]=value1
dayData.loc[idxDay, "myValue2"]=value2
Further example:
df = pd.DataFrame([1], ['a'], ['A'])
print df
A
a 1
for i, r in df.iterrows():
r.loc['B'] = 2
print df
A
a 1
for i, r in df.iterrows():
df.loc[i, 'B'] = 2
print df
A B
a 1 2.0
Clearly shows that assigning to row
does not work. Assigning to dataframe does.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.