我有以下数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame(dict(A = np.arange(3),
B = np.random.randn(3),
C = ['foo','bar','bah'],
D = pd.Timestamp('20130101')))
print(df)
A B C D
0 0 -1.087180 foo 2013-01-01
1 1 -1.343424 bar 2013-01-01
2 2 -0.193371 bah 2013-01-01
dtypes
对于列:
print(df.dtypes)
A int32
B float64
C object
D datetime64[ns]
dtype: object
但是在使用apply
它们之后,所有更改都变为对象:
print(df.apply(lambda x: x.dtype))
A object
B object
C object
D object
dtype: object
为什么dtypes
要反对?我认为apply
应该只考虑列。
pandas 0.17.1
python 3.4.3
您可以在此处使用参数reduce=False
和更多信息:
print (df.apply(lambda x: x.dtype, reduce=False))
A int32
B float64
C object
D datetime64[ns]
dtype: object
在较新版本的熊猫中可以使用:
print (df.apply(lambda x: x.dtype, result_type='expand'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句