python将空值替换成0,Python-拉伸查找最大空值并替换为0

本文介绍了如何使用Python处理DataFrame中的空值。目标是在特定日期内找到最长连续的空值序列,并将其替换为0。通过转换数据类型、创建连续分组、获取空值计数和设置条件来实现这一目标。示例中展示了处理后的数据,其中只有最长的空值序列被替换为0。
摘要由CSDN通过智能技术生成

I have dataframe with datetime and a column. I have to find maximum stretch of null values in a 'particular date' and replace it with zero.In example below, January 1st the maximum stretch null value is 3 times, so I have to replace this zero. Similarly, I have to replicate the process for 2nd January.

Note : Only the maximum stretch of null values has to be replaced with zero not the others.

Below is my sample data:

Datetime X

01-01-2018 00:00 1

01-01-2018 00:05 Nan

01-01-2018 00:10 2

01-01-2018 00:15 3

01-01-2018 00:20 2

01-01-2018 00:25 Nan

01-01-2018 00:30 Nan

01-01-2018 00:35 Nan

01-01-2018 00:40 4

02-01-2018 00:00 Nan

02-01-2018 00:05 2

02-01-2018 00:10 2

02-01-2018 00:15 2

02-01-2018 00:20 2

02-01-2018 00:25 Nan

02-01-2018

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值