我正在尝试将列表转换为数据帧,但我一直收到错误。
错误:"ValueError:传递的值的形状是(3,1),索引表示(3,8)“
之所以会发生这种情况,是因为第一列的名称中有时会有空格。有没有一种方法可以提取第一列,然后再合并回来。此列的名称中包含空格,它会打乱列计数
from selenium import webdriver
from lxml import html
driver = webdriver.Chrome()
driver.get('https://finviz.com/futures.ashx')
search_deltaprice=[]
result_elements = driver.find_elements_by_xpath('//div[@class="tile_change pull-right"]/..')
for element in result_elements:
        search_result = element.text
        search_deltaprice.append(search_result)
df = pd.DataFrame(search_deltaprice, columns =['Ticker', 'Price', 'HighLabel','HighPrice','LowLabel','LowPrice','Change','PctChange'], dtype = str)我试过这个:
df = pd.DataFrame(search_deltaprice)但这返回了一个数据帧,其中所有数据都在1列中,我希望每个数据元素都在自己的列中,空格是分隔符。类型列表没有split方法。
发布于 2020-04-28 08:36:31
尝试拆分列表中的每个字符串,并使用它来生成数据帧。对于当前示例,您需要找到要拆分的正确字符,即"\n“
driver.quit()
splitted_search=[x.split("\n") for x in search_deltaprice]
df = pd.DataFrame(splitted_search, columns =['Ticker', 'Price', 'HighLabel','HighPrice','LowLabel','LowPrice','Change','PctChange'], dtype = str)PS:记得关闭你的webdriver。
https://stackoverflow.com/questions/61470081
复制相似问题