检查多列是否为对象类型

check if multiple columns are object type

我正在导入一个 Excel 文件,当我检查 pandas 系列的数据类型时,有些是 object,有些是 int64.

我想知道传递的任何列是否属于 object 类型。

当我运行:

if df['dim_weight'].dtype == object or df['dim_length'].dtype == object or df['dim_height'].dtype == object:

并且某些列不是按预期工作的对象,但是当我执行相反操作时:

if not df['dim_weight'].dtype == object or not df['dim_length'].dtype == object or not df['dim_height'].dtype == object:

检查失败。在 Pandas 中是否有更有效的检查方法?我最终想检查是否有任何列不是字符串。非常感谢任何指导。

一般在逻辑理论中,(A or B)的反义词是(not A) and (not B),而不是(not A) or (not B)

你可以使用dataframe的属性dtypes得到一个包含所有列的数据类型的系列,然后将系列与inte64进行比较并与all进行归约以创建一个布尔值条件:

if df.dtypes.eq('int64').all():
    # Execute the statements when condition is True