python - 通过从 excel 读取数据来观察 Python 代码上的值错误
问题描述
下面是我的代码,看到相同的值错误,请提出建议。
import pandas as pd
import plotly.express as px
def create_graphs(excel_path,html_path):
df = pd.read_excel(excel_path)
fig = px.line(df, x = "Date", y = "CPU_excluding_jmeter", title='Metrics')
fig.show()
with open(html_path, 'a') as f:
f.write(fig.to_html(full_html=True))
f.close()
return
观察到的错误:
ValueError Traceback (most recent call last)
<ipython-input-93-1643babef93d> in <module>
29 return
30
---> 31 create_graphs('/home/karthika.s1/Desktop/Discovery_system_profile-Thu-Mar-19-11-03-35-UTC-20201.xls',"/home/karthika.s1/Desktop/HTML/graph3.html",)
<ipython-input-93-1643babef93d> in create_graphs(excel_path, html_path)
7
8 df = pd.read_excel(excel_path)
----> 9 fig = px.line(df, x = "Date", y = "Memory", title='Metrics')
10 fig.show()
11
~/.local/lib/python3.6/site-packages/plotly/express/_chart_types.py in line(data_frame, x, y, line_group, color, line_dash, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, line_dash_sequence, line_dash_map, log_x, log_y, range_x, range_y, line_shape, render_mode, title, template, width, height)
212 a polyline mark in 2D space.
213 """
--> 214 return make_figure(args=locals(), constructor=go.Scatter)
215
216
~/.local/lib/python3.6/site-packages/plotly/express/_core.py in make_figure(args, constructor, trace_patch, layout_patch)
1363
1364 args, trace_specs, grouped_mappings, sizeref, show_colorbar = infer_config(
-> 1365 args, constructor, trace_patch
1366 )
1367 grouper = [x.grouper or one_group for x in grouped_mappings] or [one_group]
~/.local/lib/python3.6/site-packages/plotly/express/_core.py in infer_config(args, constructor, trace_patch)
1208 all_attrables += [group_attr]
1209
-> 1210 args = build_dataframe(args, all_attrables, array_attrables)
1211 if constructor in [go.Treemap, go.Sunburst] and args["path"] is not None:
1212 args = process_dataframe_hierarchy(args)
~/.local/lib/python3.6/site-packages/plotly/express/_core.py in build_dataframe(args, attrables, array_attrables)
972 "\n To use the index, pass it in directly as `df.index`."
973 )
--> 974 raise ValueError(err_msg)
975 if length and len(df_input[argument]) != length:
976 raise ValueError(
ValueError: Value of 'y' is not the name of a column in 'data_frame'.
Expected one of ['Date', ' Memory', 'CPU', ' Memory1', ' SVC-GO-CONTENT API CPU Load', ' SVC-GO-CONTENT API Memory', ' Migration API load in ', ' Migration API Memory in ', ' Events API Load in ', ' Events API Memory in ', ' Images API Load in ', ' Images API memory in ', ' Layouts API load in ', ' Layouts API memory in ', ' Recommandations API Load', ' Recommandations API memory', ' Sharks API Load', ' Sharks API memory', ' UpNext API Load', ' UpNext API memory', ' configurations API Load', ' configurations API Memory', ' Markers API load', ' Markers API memory', ' Users API Load', ' Users API Memory', ' Entitlement API Load', ' Entitlement API Memory', ' jenkins cpu', ' jenkins memory', ' CPU_excluding_jmeter'] but received: Memory
Excel文件
当我们使用除“CPU”列之外的其他列时遇到此错误
解决方案
错误显示您有列
Expected one of ['Date', ' Memory', 'CPU', ' Memory1', ...
所以列名开头有空格,你必须尊重它
y=' Memory'
或者您必须从名称中删除空格
import pandas as pd
df = pd.DataFrame({' Memory': [], ' Memory1': []})
print('Before:', df.columns)
df.columns = df.columns.str.strip()
print(' After:', df.columns)
结果
Before: Index([' Memory', ' Memory1'], dtype='object')
After: Index(['Memory', 'Memory1'], dtype='object'
推荐阅读
- angular - Angular Reactive Forms Select 不显示初始值
- html - 无法使用 CSS Animation 为元素的宽度设置动画
- sql - 如何将一些行合并为一行,并删除其他行?
- html - 如何区分overflow-x-scroll的上div和下div
- bash - bash shell中if条件中的字符串比较错误
- java - java 匿名类可以访问外部类的私有成员。为什么此代码无法访问私有数据成员?
- c# - 在控制台应用程序中,为什么在等待的异步任务中使用同步阻塞代码 (Thread.Sleep(..)) 的行为类似于多线程?
- sql - 选择在 PostgresQl 中每月至少支付一次成功和一次失败的用户
- sql - 如何使用相同的数据填充多个报告字段?女士访问
- microsoft-graph-files - 使用 MS Graph 获取 Onedrive 文件