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

yipeiwu_com7年前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 批量修改/替换数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用python绘制3维正态分布图的方法

使用python绘制3维正态分布图的方法

今天使用python画了几个好玩的3D展示图,现在分享给大家。 先贴上图片 使用的python工具包为: from matplotlib import pyplot as pl...

Python猴子补丁知识点总结

属性在运行时的动态替换,叫做猴子补丁(Monkey Patch)。 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1.这个词原来为Guerr...

Python 条件判断的缩写方法

return (1==1) ? "is easy" : "my god" //C...

介绍Python的@property装饰器的用法

在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻...

Pytorch之contiguous的用法

contiguous tensor变量调用contiguous()函数会使tensor变量在内存中的存储变得连续。 contiguous():view只能用在contiguous的var...