3

我一直在尝试使用 and 关键字填充 DataFramefillnavalueSeries limitlimit不包括 时受到尊重,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

4

1 回答 1

3

这实际上是设计使然。limit关键字被设计为与关键字一起使用,因为method您必须指定排序(即,向前填充或回填),而您没有value.

于 2012-09-11T15:50:04.710 回答