Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python KeyError排序数据帧-无法识别列名/索引_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python KeyError排序数据帧-无法识别列名/索引

Python KeyError排序数据帧-无法识别列名/索引,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我是Python初学者。我创建了一个脚本来登录到一个网站,并从一个表中创建一个数据帧。最终,此数据框将被发送到MS Excel工作簿,但我需要首先对其进行正确排序 我的问题是对数据帧进行排序。我想按其中一列按降序对数据帧进行排序。我收到一个钥匙错误。由于某些原因,找不到列名 以下是我正在使用的代码: from bs4 import BeautifulSoup import pandas as pd from selenium import webdriver DRIVER_PATH = '<

我是Python初学者。我创建了一个脚本来登录到一个网站,并从一个表中创建一个数据帧。最终,此数据框将被发送到MS Excel工作簿,但我需要首先对其进行正确排序

我的问题是对数据帧进行排序。我想按其中一列按降序对数据帧进行排序。我收到一个钥匙错误。由于某些原因,找不到列名

以下是我正在使用的代码:

from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
DRIVER_PATH = '<PATH TO DRIVER>'
from selenium.webdriver import chrome
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(executable_path='<PATH TO DRIVER>')

#open Report Website 
driver.get('<URL OF LOGIN PAGE')

#Login to Report Website
element = driver.find_element_by_id('txtUID')
element.click()
element.clear()
element.send_keys('<MY USERNAME>')
element = driver.find_element_by_id('txtPWD')
element.click()
element.clear()
element.send_keys('<MY PASSWORD>')
import time
time.sleep(2)
element = driver.find_element_by_id('Send')
element.click()

#Navigate to Report Page
driver.get('<URL TO REPORT PAGE>')

#create df
html = driver.page_source
soup = BeautifulSoup(html)
table = soup.find_all('table')[1]
df = pd.read_html(str(table))[0]
sorted_df = df.sort_values(by='Pct Of Widgets w/ Green Label')
但我也有同样的错误。这似乎是一件很简单的事情,我不明白为什么没有找到列引用


我的具体问题是-按列降序排序数据帧的最简单方法是什么?

检查
df.columns
的输出中是否存在
8
,如果存在,则可能是int类型,因此请尝试
sorted\u df=df.sort\u value(by=8)
@AnuragDabas谢谢!这就是问题所在。嗯,也许这篇文章会对你有所帮助?
sorted_df = df.sort_values(by='8')