如果需要循环使用DataFrame.append,但这里有必要分配回输出,因为它不是pure python append:
for i, x in df2.iterrows():
df1 = df1.append(x)
df2 = df1 * 100
for i, x in df2.iterrows():
df1 = df1.append(x)
print (df1)
V1 V2 V3 A PF \
Timestamp
2017-04-01 07:00:00 254.0 243.0 246.0 1385.0 0.910143
2017-04-01 08:00:00 242.0 249.0 244.0 1058.0 0.695257
2017-04-01 09:00:00 242.0 240.0 240.0 1051.0 0.690657
2017-04-01 10:00:00 251.0 241.0 242.0 1209.0 0.794486
2017-04-01 07:00:00 25400.0 24300.0 24600.0 138500.0 91.014300
2017-04-01 08:00:00 24200.0 24900.0 24400.0 105800.0 69.525700
2017-04-01 09:00:00 24200.0 24000.0 24000.0 105100.0 69.065700
2017-04-01 10:00:00 25100.0 24100.0 24200.0 120900.0 79.448600
KVA KW
Timestamp
2017-04-01 07:00:00 594.107753 540.722928
2017-04-01 08:00:00 448.951720 312.136890
2017-04-01 09:00:00 438.093235 302.572222
2017-04-01 10:00:00 512.329064 407.038122
2017-04-01 07:00:00 59410.775300 54072.292800
2017-04-01 08:00:00 44895.172000 31213.689000
2017-04-01 09:00:00 43809.323500 30257.222200
2017-04-01 10:00:00 51232.906400 40703.812200
df2 = df1 * 100
for i, x in df2.iterrows():
df1 = df1.append(x, ignore_index=True)
print (df1)
V1 V2 V3 A PF KVA KW
0 254.0 243.0 246.0 1385.0 0.910143 594.107753 540.722928
1 242.0 249.0 244.0 1058.0 0.695257 448.951720 312.136890
2 242.0 240.0 240.0 1051.0 0.690657 438.093235 302.572222
3 251.0 241.0 242.0 1209.0 0.794486 512.329064 407.038122
4 25400.0 24300.0 24600.0 138500.0 91.014300 59410.775300 54072.292800
5 24200.0 24900.0 24400.0 105800.0 69.525700 44895.172000 31213.689000
6 24200.0 24000.0 24000.0 105100.0 69.065700 43809.323500 30257.222200
7 25100.0 24100.0 24200.0 120900.0 79.448600 51232.906400 40703.812200
编辑:
我认为更好的是创建新的 DataFrame 然后 append 只创建一次:
df2 = df1 * 100
L = []
for i, x in df2.iterrows():
x = x * 2
#append Series to list, no assign back
L.append(x)
df3 = pd.concat(L, 1).T
print (df3)
V1 V2 V3 A PF \
2017-04-01 07:00:00 50800.0 48600.0 49200.0 277000.0 182.0286
2017-04-01 08:00:00 48400.0 49800.0 48800.0 211600.0 139.0514
2017-04-01 09:00:00 48400.0 48000.0 48000.0 210200.0 138.1314
2017-04-01 10:00:00 50200.0 48200.0 48400.0 241800.0 158.8972
KVA KW
2017-04-01 07:00:00 118821.5506 108144.5856
2017-04-01 08:00:00 89790.3440 62427.3780
2017-04-01 09:00:00 87618.6470 60514.4444
2017-04-01 10:00:00 102465.8128 81407.6244
df1 = df1.append(df3)
print (df1)
V1 V2 V3 A PF \
2017-04-01 07:00:00 254.0 243.0 246.0 1385.0 0.910143
2017-04-01 08:00:00 242.0 249.0 244.0 1058.0 0.695257
2017-04-01 09:00:00 242.0 240.0 240.0 1051.0 0.690657
2017-04-01 10:00:00 251.0 241.0 242.0 1209.0 0.794486
2017-04-01 07:00:00 50800.0 48600.0 49200.0 277000.0 182.028600
2017-04-01 08:00:00 48400.0 49800.0 48800.0 211600.0 139.051400
2017-04-01 09:00:00 48400.0 48000.0 48000.0 210200.0 138.131400
2017-04-01 10:00:00 50200.0 48200.0 48400.0 241800.0 158.897200
KVA KW
2017-04-01 07:00:00 594.107753 540.722928
2017-04-01 08:00:00 448.951720 312.136890
2017-04-01 09:00:00 438.093235 302.572222
2017-04-01 10:00:00 512.329064 407.038122
2017-04-01 07:00:00 118821.550600 108144.585600
2017-04-01 08:00:00 89790.344000 62427.378000
2017-04-01 09:00:00 87618.647000 60514.444400
2017-04-01 10:00:00 102465.812800 81407.624400