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 用lambda函数替换for循环的方法

场景如下: 现在有一个dataframe,其中一列为score,值从0-100, df: score 98 88 37 68 86 33 现在需要增加一列level,给这些分数分类,90...

Python内置模块hashlib、hmac与uuid用法分析

本文实例讲述了Python内置模块hashlib、hmac与uuid用法。分享给大家供大家参考,具体如下: 一、hashlib md5和sha算法通过消息摘要算法生成定长的消息摘要,消息...

python中异常捕获方法详解

在Python中处理异常使用的是try-except代码块,try-except代码块放入让python执行的操作,同时告诉python程序如果发生了异常该怎么办,try-except这...

pytorch:torch.mm()和torch.matmul()的使用

如下所示: torch.mm(mat1, mat2, out=None) → Tensor torch.matmul(mat1, mat2, out=None) → Tensor...

简单介绍使用Python解析并修改XML文档的方法

问题 你想读取一个XML文档,对它最一些修改,然后将结果写回XML文档。 解决方案 使用 xml.etree.ElementTree 模块可以很容易的处理这些任务。 第一步是以通常的方...