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

标签 python pandas

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

df = pd.DataFrame([[0, 1], [0, 1], [0, 1]])
df.dtypes

输出为

0    int64
1    int64
dtype: object

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

最佳答案

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

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

print (type(s1))
<class 'pandas.core.series.Series'>

如果想要测试 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

如果仅测试整数 Series它返回int64并且也在 Series 的数据下显示此信息:

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

print (s.dtype)
int64

关于python - Pandas: `df.dtypes` 的最后一行是 `dtype: object` ,这是什么意思,它是谁的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162231/

相关文章:

python hist2d 未定义

python - 我怎样才能让这个Python脚本连续/一遍又一遍地运行

python - 打开 csv 文件时 pandas 中的日期时间转换错误

python - 将 pandas 数据框中的词汇表更改为值 "1"

python - pandas 数据框的两列 - Python 中的 Concat

python - 在堆积条形图中使用自定义调色板

python - 如何根据特定列在 Pandas Dataframe 中向上移动行

python - Pandas:垃圾收集丢弃的列以释放内存

python - 如何在Linux机器上用Python压缩并发送排序文件?

python - 关闭终端后停止高级 Python 调度程序作业