python - pandas.Series 中的字符串总是小于 0 吗?
问题描述
当我评估以下代码片段时,我得到了值True
import pandas as pd
df = pd.DataFrame({'a': ['assa', '100', 'AJSAND']})
(df < 0).all()
在 Pandas DataFrame 中字符串的计算结果是否总是小于零?
但是,以下导致错误
's' < 0
解决方案
如果您查看 pandas 的源代码,您可以在_comp_method_FRAME方法下找到以下几行。你可以在这里找到完整的解释。总而言之,它更多的是一种比较每种类型而不会导致异常的方法。
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 填充,然后用真正的布尔值删除!
推荐阅读
- python - 为什么 keras 的准确性和损失在不同时期之间没有变化以及如何修复
- r - 在循环中添加 xts 对象
- c# - 已采取所有已知步骤,但 TLS 1.2 仍无法在 Windows 7 上运行
- java - jvm错误的算术答案
- mysql - MySQL 存储过程返回空结果集
- css - CSS,REACT:不能将兄弟姐妹放在一行上;
- python - 如何在python中找到哪个文件有keyerror?
- reactjs - res.data 在 json 数组的末尾向我发送了一个对象
- powershell - 需要从使用 powershell 转换为文本的 CSV 列中删除所有前导零
- lua - MediaWiki 模板导入报告“错误:未指定快捷方式且未设置 |msg= 参数”