作者:嘎嘎19850820 | 来源:互联网 | 2023-10-11 10:57
我正在遍历数据框,并尝试将值添加到每一行的特定列中,但是当我打印结果数据框时,值不存在#addtwonewblankcolumnstothedayDatadataframedayD
我正在遍历数据框,并尝试将值添加到每一行的特定列中,但是当我打印结果数据框时,值不存在
#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)
value1和value 2的值是正确的,即使当我硬编码value1 = 1和value2 = 2时(当我打印dayData时,应更新myValue1和myValue2列之后的列,它们也不包含任何数据).
最终的dayData数据框应该看起来像
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
但是我得到的只是:
vwap last volume ratio myLong myShort
0 301.071871 301.221525 43133218.0 1.000497
1 215.545413 213.791400 349730738.0 0.991862
解决方法:
代替分配给行,而是分配给带有索引的数据框.该行是该行的副本,对其所做的更改将不会保留在父数据框中.
代替:
row["myValue1"]=value1
row["myValue2"]=value2
做:
dayData.loc[idxDay, "myValue1"]=value1
dayData.loc[idxDay, "myValue2"]=value2
进一步的例子:
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
清楚地表明分配给行不起作用.分配给数据框.