即使我正在比较整数,也无法对数据框中的列进行排序

时间:2019-05-10 02:25:19

标签: python-3.x pandas

我从一个曲棍球运动员的网站上刮了一张桌子,上面有他们的统计数据。默认值是按名称排序的,而我尝试按点排序。但是,即使points列都是整数(我检查过),它也不会比较表示它们是“ lxml.etree._ElementUnicodeResult”的数字。任何帮助,不胜感激!

我尝试运行带有for循环的小型冒泡排序,但是它不起作用,但是由于某些原因,当我使用索引位置0和1而不是I和I + 1时,它可以工作。我检查了一下,每个元素实际上都是类型'int',所以我不确定为什么在使用循环时它会更改。

#This was the generic bubble sort that doesn't work    
#The error is TypeError: '<' not supported between instances of 'int' and 'lxml.etree._ElementUnicodeResult'
for i in range(1,100):
    if (Dict['PTS'][i]) < (Dict['PTS'][i+1]):
        Dict['PTS'][i], Dict['PTS'][i+1] = Dict['PTS'][i+1], Dict['PTS'][I]

#But when I run this, the first two elements are switched and their are #no problems.
if (Dict['PTS'][0]) < (Dict['PTS'][1]):
    Dict['PTS'][0], Dict['PTS'][1] = Dict['PTS'][1], Dict['PTS'][0]

我尝试使用内置的sorted()和.sort_index方法,但是都具有与上面相同的错误

1 个答案:

答案 0 :(得分:0)

没关系,似乎中间隐藏着表格中的单词实例,无法比较所有元素。