python - ValueError : Length mismatch: Expected axis has 6 elements, 新值有 1 个元素

标签 python pandas matplotlib time-series

import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt

plt.style.use('ggplot')


url = "https://www.google.com/finance/historical?cid=207437&startdate=Jan%201%2C%201971&enddate=Jul%201%2C%202017&start={0}&num=30"
how_many_pages=3
start=0

for i in range(how_many_pages):
    new_url = url.format(start)
    page = requests.get(new_url)
    soup = BeautifulSoup(page.content, "lxml")
    table = soup.find_all('table', class_='gf-table historical_price')[0]

    columns_header = [th.getText() for th in table.findAll('tr')[0].findAll('th')]
    data_rows=table.findAll('tr')[1:]
    data=[[td.getText() for td in data_rows[i].findAll(['td'])] for i in range(len(data_rows))]

    if start == 0:
        final_df = pd.DataFrame(data, columns=columns_header)
    else:
        df = pd.DataFrame(data, columns=columns_header)
        final_df = pd.concat([final_df, df],axis=0)
    start += 30
    final_df.to_csv('nse_data.csv', sep='\t', encoding='utf-8')


final_df.columns = ['Date']
final_df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d', utc=True)


df.plot(x='Date', y='Close')


plt.savefig('foo.png')

下载的数据格式如下

    "Date
"   "Open
"   "High
"   "Low
"   "Close
"   "Volume
"
0   "Jun 30, 2017
"   "9,478.50
"   "9,535.80
"   "9,448.75
"   "9,520.90
"   "-
"
1   "Jun 29, 2017
"   "9,522.95
"   "9,575.80
"   "9,493.80
"   "9,504.10
"   "-

目前我只想绘制Date(在 X 轴上)与 Close(在 Y 轴上)

但是我得到了错误

ValueError:长度不匹配:预期轴有 6 个元素,新值有 1 个元素

最佳答案

  • 您的 header 和数据包含换行符。 print(final_df.columns) 返回:

    Index(['Date\n', 'Open\n', 'High\n', 'Low\n', 'Close\n', 'Volume\n'], dtype='object')
    

    使用 rstrip 去除它们:

    columns_header = [th.getText().rstrip() for th in table.findAll('tr')[0].findAll('th')]
    

    data = [[td.getText().rstrip() for td in data_rows[i].findAll(['td'])] for i in range(len(data_rows))]
    
  • final_df.columns = ['Date'] 产生您的错误。数据框需要与其列数一样多的标题。因此,在您的情况下,预计会有 6 个元素的列表。我不确定你想在这里做什么,我想你可以简单地删除这一行。

  • 您为日期解析指定的格式与您的数据不匹配 ['Apr 4, 2017', 'Apr 5, 2017', 'Apr 6, 2017',...]。关于 format codes here 的文档.改用:

    final_df['Date'] = pd.to_datetime(df['Date'], format='%b %d, %Y')
    
  • 将您的数据转换为数值以便绘制它们:

    final_df['Close'] = [float(val.replace(',', '')) for val in final_df['Close']]
    
  • 最后你可以调用:

    final_df.plot(x='Date', y='Close')
    

关于python - ValueError : Length mismatch: Expected axis has 6 elements, 新值有 1 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44876939/

相关文章:

python - 在一个函数中获取输入并在另一个函数中调用它

python - DataFrame 的复杂过滤

python - Pandas 按列值修改日期

python - 字典到数据框

python - 使用 matplotlib 显示六边形网格

python - Matplotlib basemap drawcounties 不起作用

python - 将函数参数转换为列表时出现问题

python - 使用 if 条件列表理解以获取特定类型的文件列表

python - eval() 函数中 '=' 上的 SyntaxError : invalid syntax, 子句

python - Matplotlib 条形图 - 类似于堆叠的叠加条