Why is pandas.DataFrame.apply printing out junk?

你。 提交于 2019-12-04 00:23:03

问题


Consider this simple dataframe:

   a  b
0  1  2
1  2  3

I perform a .apply as such:

In [4]: df.apply(lambda x: [x.values])
Out[4]: 
a    [[140279910807944, 140279910807920]]
b    [[140279910807944, 140279910807920]]
dtype: object

In [5]: df.apply(lambda x: [x.values])
Out[5]: 
a    [[37, 37]]
b    [[37, 37]]
dtype: object

In [6]: df.apply(lambda x: [x.values])
Out[6]: 
a    [[11, 11]]
b    [[11, 11]]
dtype: object

Why is pandas printing out junk each time?

I've verified this happens in v0.20.

Edit: Looking for an answer, not a workaround.


回答1:


It looks like bug, so was opened Issue 17487.

For me working add tolist:

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

print (df.apply(lambda x: [list(x.values)]))
a    [[1, 2]]
b    [[2, 3]]
dtype: object



回答2:


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)



回答3:


Another work around:

df.apply(lambda x: [list(x)])


来源:https://stackoverflow.com/questions/46137355/why-is-pandas-dataframe-apply-printing-out-junk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!