字符串在吗Pandas系列总是小于0?

2024-09-30 00:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

当我计算下面的代码片段时,得到值True

import pandas as pd
df = pd.DataFrame({'a': ['assa', '100', 'AJSAND']})
(df < 0).all()

在数据帧中字符串的计算值总是小于零吗?你知道吗

但是,以下操作会导致错误

's' < 0

Tags: 数据字符串代码importtruedataframepandasdf
1条回答
网友
1楼 · 发布于 2024-09-30 00:36:18

如果您检查pandas的源代码,您可以在comp\method\FRAME方法下找到以下行。你可以找到一个完整的解释here。总之,它更多的是一种在不产生异常的情况下比较每种类型的方法。你知道吗

def _comp_method_FRAME(cls, func, special):
    str_rep = _get_opstr(func, cls)
    op_name = _get_op_name(func, special)

    @Appender('Wrapper for comparison method {name}'.format(name=op_name))
    def f(self, other):
        if isinstance(other, ABCDataFrame):
            # Another DataFrame
            if not self._indexed_same(other):
                raise ValueError('Can only compare identically-labeled '
                                 'DataFrame objects')
            return self._compare_frame(other, func, str_rep)

        elif isinstance(other, ABCSeries):
            return _combine_series_frame(self, other, func,
                                         fill_value=None, axis=None,
                                         level=None, try_cast=False)
        else:

            # straight boolean comparisons we want to allow all columns
            # (regardless of dtype to pass thru) See #4537 for discussion.
            res = self._combine_const(other, func,
                                      errors='ignore',
                                      try_cast=False)
            return res.fillna(True).astype(bool)

    f.__name__ = op_name

    return f

所以基本上,数据帧首先填充nan,然后用真布尔值删除!你知道吗

相关问题 更多 >

    热门问题