Pandas:`df.dtypes`的最后一行是`dtype: object`,这是什么意思,它是谁的类型?

10 python pandas

这段代码构造了一个简单的DataFrame

df = pd.DataFrame([[0, 1], [0, 1], [0, 1]])
df.dtypes
Run Code Online (Sandbox Code Playgroud)

输出是

0    int64
1    int64
dtype: object
Run Code Online (Sandbox Code Playgroud)

输出的最后一行是dtype: object,这是什么意思,它是谁的类型?

jez*_*ael 2

这意味着Series返回了df.dtypeshas dtype object,因为至少有 obne 类型的 Series 在object这里<class 'numpy.dtype'>

s1 = df.dtypes
print (s1.dtype)
object

print (type(s1))
<class 'pandas.core.series.Series'>
Run Code Online (Sandbox Code Playgroud)

如果想要测试每个元素的类型Series

print (s1.apply(type))
MPG                                         <class 'numpy.dtype'>
Cylinders                                   <class 'numpy.dtype'>
Displacement                                <class 'numpy.dtype'>
Horsepower                                  <class 'numpy.dtype'>
Weight                                      <class 'numpy.dtype'>
Acceleration                                <class 'numpy.dtype'>
Year                                        <class 'numpy.dtype'>
Origin          <class 'pandas.core.dtypes.dtypes.CategoricalD...
dtype: object
Run Code Online (Sandbox Code Playgroud)

如果仅测试整数,Series它会返回int64并且还会在以下数据下显示此信息Series

s = pd.Series([1,2])
print (s)
0    1
1    2
dtype: int64

print (s.dtype)
int64
Run Code Online (Sandbox Code Playgroud)