我在以下代码中创建df['var'2]
并更改df['var1']
。执行这些更改后,我想将newrow
(带df['var'2]
)附加到数据框,同时保留原始(虽然现在已更改)行(具有df['var1']
)。
for i, row in df.iterrows():
while row['var1'] > 30:
newrow = row
newrow['var2'] = 30
row['var1'] = row['var1']-30
df.append(newrow)
我理解在使用iterrows()
时,行变量是副本而不是视图,这就是原始数据框中未更新更改的原因。那么,我如何更改此代码以实际将新内容附加到数据框?
谢谢!
答案 0 :(得分:3)
将行追加到循环中的数据框通常是低效的,因为返回了新副本。最好将中间结果存储在列表中,然后在最后将所有内容连接在一起。
使用row.loc['var1'] = row['var1'] - 30
将对原始数据框进行原位更改。
np.random.seed(0)
df = pd.DataFrame(np.random.randn(5, 2) * 100, columns=['var1', 'var2'])
>>> df
var1 var2
0 176.405235 40.015721
1 97.873798 224.089320
2 186.755799 -97.727788
3 95.008842 -15.135721
4 -10.321885 41.059850
new_rows = []
for i, row in df.iterrows():
while row['var1'] > 30:
newrow = row
newrow['var2'] = 30
row.loc['var1'] = row['var1'] - 30
new_rows.append(newrow.values)
df_new = df.append(pd.DataFrame(new_rows, columns=df.columns)).reset_index()
>>> df
var1 var2
0 26.405235 30.00000
1 7.873798 30.00000
2 6.755799 30.00000
3 5.008842 30.00000
4 -10.321885 41.05985
>>> df_new
var1 var2
0 26.405235 30.00000
1 7.873798 30.00000
2 6.755799 30.00000
3 5.008842 30.00000
4 -10.321885 41.05985
5 26.405235 30.00000
6 26.405235 30.00000
7 26.405235 30.00000
8 26.405235 30.00000
9 26.405235 30.00000
10 7.873798 30.00000
11 7.873798 30.00000
12 7.873798 30.00000
13 6.755799 30.00000
14 6.755799 30.00000
15 6.755799 30.00000
16 6.755799 30.00000
17 6.755799 30.00000
18 6.755799 30.00000
19 5.008842 30.00000
20 5.008842 30.00000
21 5.008842 30.00000
编辑(根据以下要求):
new_rows = []
for i, row in df.iterrows():
while row['var1'] > 30:
row.loc['var1'] = var1 = row['var1'] - 30
new_rows.append([var1, 30])
df_new = df.append(pd.DataFrame(new_rows, columns=df.columns)).reset_index()
>>> df_new
index var1 var2
0 0 26.405235 40.015721
1 1 7.873798 224.089320
2 2 6.755799 -97.727788
3 3 5.008842 -15.135721
4 4 -10.321885 41.059850
5 0 146.405235 30.000000
6 1 116.405235 30.000000
7 2 86.405235 30.000000
8 3 56.405235 30.000000
9 4 26.405235 30.000000
10 5 67.873798 30.000000
11 6 37.873798 30.000000
12 7 7.873798 30.000000
13 8 156.755799 30.000000
14 9 126.755799 30.000000
15 10 96.755799 30.000000
16 11 66.755799 30.000000
17 12 36.755799 30.000000
18 13 6.755799 30.000000
19 14 65.008842 30.000000
20 15 35.008842 30.000000
21 16 5.008842 30.000000