Python:foward填充nans和0
发布时间:2020-12-20 12:01:46 所属栏目:Python 来源:网络整理
导读:假设我有一个数据帧df1,它有零和nans: 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
假设我有一个数据帧df1,它有零和nans:
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.nan df1.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 see 2017-01-01 2.0 1.0 4 2017-01-02 2.0 2.0 6 2017-01-03 2.0 8.0 3 2017-01-04 2.0 6.0 1 2017-01-05 2.0 8.0 4 2017-01-06 2.0 9.0 6 2017-01-07 2.0 9.0 8 2017-01-08 2.0 9.0 5 2017-01-09 2.0 9.0 8 2017-01-10 2.0 9.0 7 2017-01-11 2.0 9.0 3 2017-01-12 2.0 9.0 6 2017-01-13 5.0 9.0 4 2017-01-14 6.0 9.0 9 2017-01-15 7.0 9.0 4 2017-01-16 6.0 9.0 2 2017-01-17 2.0 9.0 5 2017-01-18 3.0 1.0 1 2017-01-19 3.0 8.0 1 2017-01-20 2.0 5.0 7 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |