Fill nan with repeated values

不问归期 提交于 2021-01-28 09:07:24

问题


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

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.

Any ideas ? Thanks !


回答1:


Just do :

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

Then

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


来源:https://stackoverflow.com/questions/42363235/fill-nan-with-repeated-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!