1
在遍歷熊貓數據框並進行更改時,更新數據框的更好方法是什麼?下面一個例子,現在我使用的索引ix
定位行更新,我相信不是,特別是如果數據是大的最好方法:更新熊貓數據框並更新數據幀的更好方法
print df
id | A | B
01 | 374 | 2014-02-01 04:45:04.401502
02 | 284 | 2014-03-12 21:23:12.401502
03 | 183 | 2014-02-01 09:12:08.401502
for row in df.itertuples():
id = row[1]
col_a = row[2]
col_b = row[3]
N = random.randint(2,5)
for i in xrange(0, N):
new_col_a = col_a + 1
new_col_b = datetime.datetime.now()
#update dataframe's A, B respectively
df.ix[df['id'] == id, ['A', 'B']] = [col_a, col_b]
print df
id | A | B
01 | 374 | 2014-02-01 04:45:04.401502
01 | 375 | 2016-12-07 07:45:04.401502
01 | 376 | 2016-12-07 07:45:04.401502
01 | 377 | 2014-12-07 07:45:04.401502
02 | 284 | 2014-03-12 21:23:12.401502
02 | 285 | 2016-12-07 07:45:04.401502
02 | 286 | 2016-12-07 07:45:04.401502
03 | 183 | 2014-02-01 09:12:08.401502
03 | 184 | 2016-12-07 07:45:04.401502
03 | 185 | 2016-12-07 07:45:04.401502
03 | 186 | 2016-12-07 07:45:04.401502
看來你只是想改變列名 –