繁体   English   中英

在 for 中的 Python 列表中返回等于或小于 6 的数字

[英]Returning numbers equal to or less than 6 in a Python list within a for


我只需要在此 FOR 中返回每列中等于或小于 6 的值。

TypeError                                 Traceback (most recent call last)
<ipython-input-120-364994f742fd> in <module>()
      4    nome_coluna = coluna
      5    #total_parcial = df2[coluna].count()
----> 6    df2.loc[df2[coluna]<=6].shape[0]
      7    percentual = df2[coluna].count() / df2[coluna].count()
      8    lista.append([nome_coluna,total_parcial,percentual])

3 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/ops/array_ops.py in comp_method_OBJECT_ARRAY(op, x, y)
     54         result = libops.vec_compare(x.ravel(), y.ravel(), op)
     55     else:
---> 56         result = libops.scalar_compare(x.ravel(), y, op)
     57     return result.reshape(x.shape)
     58 

pandas/_libs/ops.pyx in pandas._libs.ops.scalar_compare()

TypeError: '<=' not supported between instances of 'str' and 'int'


但返回错误

df2.loc[df2['Pontualidade'] <= 6].shape[0]

1537

如果我将单独给出错误的代码放在一行中,它就可以工作

df2.loc[df2['Pontualidade'] <= 6].shape[0] 1537

什么是正确的语法? 谢谢

您的一列或部分列具有非数字值。 如果您确定所有列都应该是数字,请使用df2[column_name] = pandas.to_numeric(df2[column_name])

确保没有数字字符串,如“123”,混合在那里。

首先,你的语法是正确的。 该错误与类型有关。 似乎您的某些列中包含字符串而不是数字,这在与数字进行比较时会导致此错误。 您可以使用df2.dtypes检查列的类型。

您测试的其中一列是否可能包含字符串而不是数字? 这将解释抛出的错误。 一个好的调试步骤是在循环开始时打印列名,以查看它在哪个迭代中失败。

您的 DataFrame 的列之一包含字符串,而不是数字。 如果每一列都应该是数字,您可以通过将.astype(float)添加到比较的左侧来将行转换为数字,即,

df2.loc[df2[coluna].astype(float)<=6].shape[0]
# Will return the number of rows with values greater than 6

但是最好在代码中更早地找出为什么您希望为数字的列不是数字列。 请注意,如果您的列包含无法转换为浮点数的值,这仍然会引发错误。

顺便说一句,由于比较将作为一系列布尔值返回,您可以通过简单地取布尔值的总和来简化和阐明代码,即

(df2[coluna].astype(float)<=6).sum()
# Will also return the number of rows with values greater than 6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM