用重复值填充 nan [英] Fill nan with repeated values

查看:91
本文介绍了用重复值填充 nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个包含 nans 的数据框:

I have this dataframe which includes nans:

     Date
0    7.0  
1    8.0    
2    9.0    
3   10.0     
4   11.0   
5   12.0  
6    1.0  
7    2.0    
8    3.0    
9    4.0    
10   5.0 
11   6.0 
   ...
90   NaN
91   NaN

日期值是月份数,我知道在索引 90 上它是 1,但我想用 2、3 等填充其他 NaN,直到 12 然后返回到 1、2 等等.让我们说就像在 Excel 中一样,当你想填充一列时,你输入第一个值,然后选择它们并一直滑动,它会自动填充.

The Date values are the month number, and I know that on the index 90 it is a 1 but I want to fill the other NaNs with 2, 3 etc until 12 then goes back to 1, 2 and so on. Lets say it just like in Excel, when you want to fill a column, you put the first values then select them and slide all the way and it fills automatically.

有什么想法吗?谢谢!

推荐答案

Just do :

df.Date=(range(len(df))+df.Date.loc[0]-1)%12+1

然后

In [9]: df.loc[[0,90]]
Out[9]: 
    Date
0    7.0
90   1.0

这篇关于用重复值填充 nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆