简体   繁体   English

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

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


I need to return within this FOR only values equal to or less than 6 in each column. 我只需要在此 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'


But returns the error但返回错误

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

1537

If I put the code that is giving the error alone in a line it works如果我将单独给出错误的代码放在一行中,它就可以工作

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

What is the correct syntax?什么是正确的语法? Thanks谢谢

One or some of your columns has non-numeric values.您的一列或部分列具有非数字值。 If you are sure the columns all should be numeric, use df2[column_name] = pandas.to_numeric(df2[column_name])如果您确定所有列都应该是数字,请使用df2[column_name] = pandas.to_numeric(df2[column_name])

to make sure that no number strings, like "123", are mixed in there.确保没有数字字符串,如“123”,混合在那里。

First, your syntax there is correct.首先,你的语法是正确的。 The error is related to types.该错误与类型有关。 It seems that some of your columns have strings instead of numbers in them, which would cause this error when comparing to a number.似乎您的某些列中包含字符串而不是数字,这在与数字进行比较时会导致此错误。 You can check the type of the columns with df2.dtypes .您可以使用df2.dtypes检查列的类型。

Is it possible that one of the columns you test contains strings instead of numbers?您测试的其中一列是否可能包含字符串而不是数字? That would explain the thrown error.这将解释抛出的错误。 A good debugging-step would be to print the column-name at the beginning of the loop to see in which iteration it fails.一个好的调试步骤是在循环开始时打印列名,以查看它在哪个迭代中失败。

One of your DataFrame's columns contains strings, rather than numbers.您的 DataFrame 的列之一包含字符串,而不是数字。 If every column is supposed to be numeric, you can cast the rows to numbers by adding .astype(float) to the left side of the comparison, ie,如果每一列都应该是数字,您可以通过将.astype(float)添加到比较的左侧来将行转换为数字,即,

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

but it is probably better to find out why a column you expect to be numeric, isn't, earlier in your code.但是最好在代码中更早地找出为什么您希望为数字的列不是数字列。 Note that this will still raise an error if your column contains values that can't be cast to floating-point numbers.请注意,如果您的列包含无法转换为浮点数的值,这仍然会引发错误。

As an aside, as the comparison will return as series of booleans, you can simplify and clarify the code by simply taking a sum of the booleans, ie顺便说一句,由于比较将作为一系列布尔值返回,您可以通过简单地取布尔值的总和来简化和阐明代码,即

(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