python - 如何修复“ValueError:长度不匹配”
问题描述
我收到一个错误:
ValueError: Length mismatch: Expected axis has 5 elements, new values have 6
elements
当我运行我的基本代码时:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
READ = "C:\\Users\\Ashley\\Biotech Data Center 4.1.21.xlsx"
stocks = "BHVN"
PipelineData = pd.read_excel(READ, sheet_name='Sheet1', header=None, index_col=0)
pipelinecolstitle = pd.read_excel(READ, sheet_name='Sheet1', header=None,nrows=1).values[0]
PipelineData.columns = pipelinecolstitle
colspipe= ['Catalyst']
tt = PipelineData[colspipe]
for i in stocks:
t = tt.loc[(PipelineData['Ticker']==i)]
print(t)
fig, ax = plt.subplots()
cols = ["Catalyst"]
axMain = plt.subplot(2,1,1)
table = plt.subplot(2,1,2, frameon=False)
axMain.plot([1,2,7])
table = ax.table(cellText=t, loc='upper center',colLabels=cols)
table.axis("off")
plt.show()
似乎它不喜欢 index_col=0 如果我更改 index_col=None 它会给我一个 KeyError: 0
解决方案
这个错误可能是由这个 assignment 引发的,这PipelineData.columns = pipelinecolstitle
意味着PipelineData的列数不等于pipelinecolstitle长度。
检查PipelineData.shape[1] == len(pipelinecolstitle)
推荐阅读
- git - 非交互式方式来压缩一系列不从 HEAD 开始的 git 提交
- python - Python在没有日期时间库的情况下添加日期
- spring-security - Spring SAML 默认确认 URL | 调整
- c# - 使用 linq C# 仅在同一天和 23:59 附近将记录获取到记录中
- javascript - 从字符串中切出单词
- java - 将工件上传到 gitlab maven 存储库
- spinnaker - Spinnaker 配置作为代码从 gitlab 加载
- html - 使导航栏超过最大宽度 [CSS]
- angular-cli - Angular CLI 未安装
- bash - grep:使用 while 循环时重复计数无效