编辑:
我有(不是很简单)一个数据框:
df = pd.DataFrame([1, 2, np.nan, np.nan, np.nan, np.nan, 3, 4
, np.nan, np.nan, np.nan, 5], columns=['att1'])
att1
0 1.0000
1 2.0000
2 nan
3 nan
4 nan
5 nan
6 3.0000
7 4.0000
8 nan
9 nan
10 nan
11 5.0000
我要用上一个非NAN
值填充NAN
值,但最后一个NAN
值除外。我希望最后一个NAN
值是填充后的NAN
。我该怎么做?
我想要这个结果:
att1
0 1.0000
1 2.0000
2 2.0000
3 2.0000
4 2.0000
5 nan
6 3.0000
7 4.0000
8 4.0000
9 4.0000
10 nan
11 5.0000
我试过这个:
df = df.fillna(value='missing', method='bfill', limit=1)
df = df.fillna(method='ffill')
但是第一行给出了这个错误:
ValueError: cannot specify both a fill method and value
为什么pandas 0.17.1/Python 3.5中有这个限制? 谢谢您!
您可以在} 的参数:
df['att1']
中计数NaN
,减法1
,然后它用作limits
到^{编辑:
现在情况更复杂了。
因此,首先设置helper column} 、^{} 、^{} 和^{} 计算列} 按此列} :
count
,用于通过^{att1
的连续值。然后^{count
和^{解释列
count
:另一种可能不太复杂的方法是创建一个索引点列表,在该列表中您希望有nan(其中索引点不为空,但前面的索引点为空)。然后,您只需向前填充数据并使用创建的列表重新插入nan。
菲娜所有的南与“失踪”。 最后一个“失踪”你可以换成南。
反向使用iloc搜索索引的负值。-2返回“att1”列的前一个元素的值。
相关问题 更多 >
编程相关推荐