Why is pandas.DataFrame.apply printing out junk?

前端 未结 3 1764
谎友^
谎友^ 2021-01-11 19:08

Consider this simple dataframe:

   a  b
0  1  2
1  2  3

I perform a .apply as such:

In [4]: df.apply(lambda x:         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-11 19:28

    I don't have an answer... just a work around

    f = lambda x: x.values.reshape(1, -1).tolist()
    
    df.apply(f)
    
    a    [[1, 2]]
    b    [[2, 3]]
    dtype: object
    

    I tracked it down to pd.lib.reduce

    pd.lib.reduce(df.values, lambda x: [list(x)])
    
    array([list([[1, 2]]), list([[2, 3]]), list([['a', 'b']])], dtype=object)
    

    Versus

    pd.lib.reduce(df.values, lambda x: [x])
    
    array([list([array([None, None], dtype=object)]),
           list([array([None, None], dtype=object)]),
           list([array([None, None], dtype=object)])], dtype=object)
    

提交回复
热议问题