从数据框到数据透视表时,Pandas处理缺失值

Pau*_*aul 10 python pivot-table pandas

鉴于以下pandas数据框:

df = pd.DataFrame({'A': ['foo' ] * 3 + ['bar'],
         'B': ['w','x']*2,
         'C': ['y', 'z', 'a','a'],
         'D': rand.randn(4),
          })

print df.to_string()
"""
     A  B  C           D
0  foo  w  y  0.06075020
1  foo  x  z  0.21112476
2  foo  w  a  0.01652757
3  bar  x  a  0.17718772
"""
Run Code Online (Sandbox Code Playgroud)

注意没有bar,w组合.执行以下操作时:

pv0 = pandas.pivot_table(df, rows=['A','B'],cols=['C'], aggfunc=numpy.sum)

pv0.ix['bar','x'] #returns result

pv0.ix['bar','w'] #key error though i would like it to return all Nan's

pv0.index #returns 
[(bar, x), (foo, w), (foo, x)]
Run Code Online (Sandbox Code Playgroud)

只要在'C'列中至少有一个条目与foo,x的情况一样(它在'C'列中只有'z'的值),它将为其他列值返回NaN' C'不存在于foo,x(例如'a','y')

我想要的是拥有所有多索引组合,即使那些没有所有列值数据的组合.

pv0.index #I would like it to return
[(bar, w), (bar, x), (foo, w), (foo, x)]
Run Code Online (Sandbox Code Playgroud)

我可以将.ix命令包装在try/except块中,但有没有一种方法可以让pandas自动填充它?

Rom*_*kar 7

你可以使用reindex()方法:

>>> df1 = pd.pivot_table(df, rows=['A','B'], cols='C', aggfunc=np.sum)
>>> df1
              D                   
C             a        y         z
A   B                             
bar x  0.161702      NaN       NaN
foo w  0.749007  0.85552       NaN
    x       NaN      NaN  0.458701

>>> index = list(iter.product(df['A'].unique(), df['B'].unique()))
>>> df1.reindex(index)
              D                   
C             a        y         z
foo w  0.749007  0.85552       NaN
    x       NaN      NaN  0.458701
bar w       NaN      NaN       NaN
    x  0.161702      NaN       NaN
Run Code Online (Sandbox Code Playgroud)