0

我正在尝试使用循环在我的 df 中创建一个新列。完成后,我收到以下错误消息:

TypeError:“str”和“int”的实例之间不支持“<”

我正在尝试的代码如下:

 # Create a list to store the data
estrat = []

# For each row in the column,
for row in cov19['cases']:
    # if more than a value,
    if row < 10:
        # Append a letter grade
        cov19.append('Very Low')
    # else, if more than a value,
    elif row > 10 and row < 50:
        # Append a letter grade
        cov19.append('Low')
    # else, if more than a value,
    elif row > 50 and row < 100:
        # Append a letter grade
        cov19.append('Medium')
    # else, if more than a value,
    elif row > 100 and row < 1000:
        # Append a letter grade
        cov19.append('High')
    # else, if more than a value,
    else:
        # Append a failing grade
        cov19.append('Very High')

# Create a column from the list
cov19['estrat'] = estart 

有什么帮助吗?谢谢,

4

1 回答 1

0

Your data row are stored as text. You need to convert to int type before testing it.

This can be done using:

if int(row) < 10:
于 2020-05-21T07:54:44.017 回答