python 批量修改/替换数据的实例

在进行数据操作时,经常会根据条件批量的修改数据,如以下数据,按照日期的条件,将部门日期下的promotion改为1

tot_qty price date price_delta1 price_delta2 price_delta3 promotion
created_date
20160419 1.0 5.410000 20160419 NaN NaN NaN 0
20161111 96.0 5.400000 20161111 -0.010000 NaN NaN 1
20161123 1.0 7.500000 20161123 2.100000 2.090000 NaN 0
20161213 5.0 7.500000 20161213 0.000000 2.100000 2.090000 0
20161226 37.0 6.258571 20161226 -1.241429 -1.241429 0.858571 0

方法:遍历每一行,判断条件,执行修改

for i in range(len(data)):
 line=data.iloc[i,:]
 if line['date']in (20170416,20170417,20170418,20170616,20170617,20170618,20170816,20170817,20170818,20171109,20171110,20171111):
 data[i:i+1]['promotion']=1

以上这篇python 批量修改/替换数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

代码技巧

转载请关注公众号:代码技巧 回复:授权

本文链接地址:https://www.oudahe.com/p/54514/