我的import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
看起来像这样:
pd.Series
如何使用系列中之前的非NaN值填充NaN列值?
我试过这个:
>>> series
0 This is a foo bar something...
1 NaN
2 NaN
3 foo bar indeed something...
4 NaN
5 NaN
6 foo your bar self...
7 NaN
8 NaN
但在new_column = []
for row in list(series):
if type(row) == str:
new_column.append(row)
else:
new_column.append(new_column[-1])
series = pd.Series(new_column)
还有另一种方法吗?
答案 0 :(得分:2)
来自docs:
DataFrame.fillna(value = None,method = None,axis = None,inplace = False,limit = None,downcast = None,** kwargs)
...
方法:{'backfill','bfill','pad','ffill',无},默认无
用于填充重新索引的填充孔的方法系列填充/填充:将最后一次有效观察传播到下一个有效回填/填充:使用NEXT有效观察来填补空白
所以:
series.fillna(method='ffill')
一些解释:
ffill
/ pad
:转发填充是使用上一行中不是NA的值并填充NA值。 pad
只是ffill
的详细别名。
bfill
/ backfill
:返回填充是使用下一行中的值来填充NA值。 backfill
只是bfill
的详细别名。
代码:
>>> import pandas as pd
>>> import numpy as np
>>> np.NaN
nan
>>> series = pd.Series([np.NaN, 'abc', np.NaN, np.NaN, 'def', np.NaN, np.NaN])
>>> series
0 NaN
1 abc
2 NaN
3 NaN
4 def
5 NaN
6 NaN
dtype: object
>>> series.fillna(method='ffill')
0 NaN
1 abc
2 abc
3 abc
4 def
5 def
6 def
dtype: object
>>> series.fillna(method='bfill')
0 abc
1 abc
2 def
3 def
4 def
5 NaN
6 NaN
dtype: object