
dates = pd.date_range('20170101',periods=20)df1 = pd.DataFrame(np.random.randint(10,size=(20,3)),index=dates,columns=['foo','bar','see'])df1.iloc[3:12,0] = np.nandf1.iloc[6:17,1] = 0 什么是前进填充zeors和nans的简洁方法?我试过以下:
df1 = (df1.fillna(method='ffill',inplace=True)).replace(to_replace=0,method='ffill')AttributeError: 'nonetype' object has no attribute 'replace'解决方法 让我们使用replace来替换为nan的零然后填充:
df1.replace(0,pd.np.nan).ffill()
输出:
foo bar see2017-01-01 2.0 1.0 42017-01-02 2.0 2.0 62017-01-03 2.0 8.0 32017-01-04 2.0 6.0 12017-01-05 2.0 8.0 42017-01-06 2.0 9.0 62017-01-07 2.0 9.0 82017-01-08 2.0 9.0 52017-01-09 2.0 9.0 82017-01-10 2.0 9.0 72017-01-11 2.0 9.0 32017-01-12 2.0 9.0 62017-01-13 5.0 9.0 42017-01-14 6.0 9.0 92017-01-15 7.0 9.0 42017-01-16 6.0 9.0 22017-01-17 2.0 9.0 52017-01-18 3.0 1.0 12017-01-19 3.0 8.0 12017-01-20 2.0 5.0 7总结
以上是内存溢出为你收集整理的Python:foward填充nans和0全部内容,希望文章能够帮你解决Python:foward填充nans和0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)