我一直在尝试使用 and 关键字填充 DataFramefillna
和value
Series limit
。limit
不包括 时受到尊重,value
但一旦包括value
限制则不再受到尊重。这是一个使用 DataFrame 的示例:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],columns=['one', 'two','three'])
df2 = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g','h','i', 'j','k'])
In [7]: df2
Out[7]:
one two three
a -0.942695 0.465658 -0.966754
b NaN NaN NaN
c -1.208036 0.287274 -1.116466
d NaN NaN NaN
e 0.041212 0.065966 -1.895570
f 0.869104 -3.481962 -0.280699
g NaN NaN NaN
h -1.151732 -0.310296 -1.701202
i NaN NaN NaN
j NaN NaN NaN
k NaN NaN NaN
In [8]: df2.fillna(method='pad', limit=1)
Out[8]:
one two three
a -0.942695 0.465658 -0.966754
b -0.942695 0.465658 -0.966754
c -1.208036 0.287274 -1.116466
d -1.208036 0.287274 -1.116466
e 0.041212 0.065966 -1.895570
f 0.869104 -3.481962 -0.280699
g 0.869104 -3.481962 -0.280699
h -1.151732 -0.310296 -1.701202
i -1.151732 -0.310296 -1.701202
j NaN NaN NaN
k NaN NaN NaN
In [9]: df2.fillna(value=999,method='pad', limit=1)
Out[9]:
one two three
a -0.942695 0.465658 -0.966754
b 999.000000 999.000000 999.000000
c -1.208036 0.287274 -1.116466
d 999.000000 999.000000 999.000000
e 0.041212 0.065966 -1.895570
f 0.869104 -3.481962 -0.280699
g 999.000000 999.000000 999.000000
h -1.151732 -0.310296 -1.701202
i 999.000000 999.000000 999.000000
j 999.000000 999.000000 999.000000
k 999.000000 999.000000 999.000000
我在这里遗漏了什么,还是这是一个错误?
干杯
编辑:在 python 2.7 上使用 pandas 0.8.1 和 numpy 1.6.1