簡體   English   中英

通過從 excel 讀取數據觀察 Python 代碼的值錯誤

[英]Observing Value Error on Python code by reading data from excel

下面是我的代碼,看到相同的值錯誤,請提出建議。

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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM