为什么pandas.dataframe.apply打印出垃圾



考虑此简单的数据框:

   a  b
0  1  2
1  2  3

我执行.apply这样:

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

为什么大熊猫每次都会打印出垃圾?

我已经验证了这件事发生在v0.20中。

编辑:寻找答案,而不是解决方法。

它看起来像错误,因此打开了第17487页。

对我来说,工作添加 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

我没有答案...只是解决

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

我将其跟踪到pd.lib.reduce

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

vers

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)

另一个工作:

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

最新更新