考虑这个简单的数据框:
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
为什么 pandas 每次都打印出垃圾?
我已经证实这发生在 v0.20 中。
编辑:寻找答案,而不是解决方法。
最佳答案
看起来像bug,所以被打开了Issue 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
关于python - 为什么 pandas.DataFrame.apply 打印出垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137355/