ValueError: Value of 'color' is not the name of a column in 'data_frame when plotting a stacked bar chart using plotly

Viewed 22

I want to find the statistics based on the categorical variable in admin.disease_code column and then plot a stacked bar chart. My code raised ValueError: Value of 'color' is not the name of a column in 'data_frame'. Expected one of ['count'] but received: admin.disease_code error.

import pandas as pd
import numpy as np
import plotly.express as px

agg_df = df.groupby(['admin.disease_code','patient.days_to_death','patient.age_at_initial_pathologic_diagnosis','patient.karnofsky_performance_score', 'survival'])['admin.disease_code'].agg({'count'})

subtype_df = agg_df.groupby(level=0).apply(lambda x:100 * x / float(x.sum()))

subtype_df.rename(columns = {'patient.days_to_death':'days_to_death', 'patient.vital_status':'vital_status', 'patient.age_at_initial_pathologic_diagnosis':'age_at_initial_pathologic_diagnosis', 'patient.gender':'gender','patient.karnofsky_performance_score':'karnofsky_performance_score'}, inplace = True)

# Plot
fig = px.bar(subtype_df, x=subtype_df.columns, y="count", color="admin.disease_code", title="Clinical information for RCC subtypes")
fig.show()

Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_17/1013923963.py in <module>
----> 1 fig = px.bar(subtype_df, x=subtype_df.columns, y="count", color="admin.disease_code", title="Clinical information for RCC subtypes")
      2 fig.show()

/opt/conda/lib/python3.7/site-packages/plotly/express/_chart_types.py in bar(data_frame, x, y, color, pattern_shape, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, hover_name, hover_data, custom_data, text, base, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, color_continuous_scale, pattern_shape_sequence, pattern_shape_map, range_color, color_continuous_midpoint, opacity, orientation, barmode, log_x, log_y, range_x, range_y, text_auto, title, template, width, height)
    375         constructor=go.Bar,
    376         trace_patch=dict(textposition="auto"),
--> 377         layout_patch=dict(barmode=barmode),
    378     )
    379 

/opt/conda/lib/python3.7/site-packages/plotly/express/_core.py in make_figure(args, constructor, trace_patch, layout_patch)
   1988     apply_default_cascade(args)
   1989 
-> 1990     args = build_dataframe(args, constructor)
   1991     if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   1992         args = process_dataframe_hierarchy(args)

/opt/conda/lib/python3.7/site-packages/plotly/express/_core.py in build_dataframe(args, constructor)
   1404 
   1405     df_output, wide_id_vars = process_args_into_dataframe(
-> 1406         args, wide_mode, var_name, value_name
   1407     )
   1408 

/opt/conda/lib/python3.7/site-packages/plotly/express/_core.py in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1205                         if argument == "index":
   1206                             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1207                         raise ValueError(err_msg)
   1208                 elif length and len(df_input[argument]) != length:
   1209                     raise ValueError(

ValueError: Value of 'color' is not the name of a column in 'data_frame'. Expected one of ['count'] but received: admin.disease_code

df.head().to_dict()

{'admin.disease_code': {'TCGA-2K-A9WE-01A-11D-A381-01': 'kirp',
  'TCGA-2Z-A9J1-01A-11D-A381-01': 'kirp',
  'TCGA-2Z-A9J2-01A-11D-A381-01': 'kirp',
  'TCGA-2Z-A9J3-01A-12D-A381-01': 'kirp',
  'TCGA-2Z-A9J5-01A-21D-A381-01': 'kirp'},
 'patient.days_to_death': {'TCGA-2K-A9WE-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J1-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J2-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J3-01A-12D-A381-01': '1771',
  'TCGA-2Z-A9J5-01A-21D-A381-01': nan},
 'patient.age_at_initial_pathologic_diagnosis': {'TCGA-2K-A9WE-01A-11D-A381-01': '53',
  'TCGA-2Z-A9J1-01A-11D-A381-01': '71',
  'TCGA-2Z-A9J2-01A-11D-A381-01': '71',
  'TCGA-2Z-A9J3-01A-12D-A381-01': '67',
  'TCGA-2Z-A9J5-01A-21D-A381-01': '80'},
 'patient.karnofsky_performance_score': {'TCGA-2K-A9WE-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J1-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J2-01A-11D-A381-01': nan,
  'TCGA-2Z-A9J3-01A-12D-A381-01': nan,
  'TCGA-2Z-A9J5-01A-21D-A381-01': nan},
 'tcga_id': {'TCGA-2K-A9WE-01A-11D-A381-01': 'TCGA-2K-A9WE-01A',
  'TCGA-2Z-A9J1-01A-11D-A381-01': 'TCGA-2Z-A9J1-01A',
  'TCGA-2Z-A9J2-01A-11D-A381-01': 'TCGA-2Z-A9J2-01A',
  'TCGA-2Z-A9J3-01A-12D-A381-01': 'TCGA-2Z-A9J3-01A',
  'TCGA-2Z-A9J5-01A-21D-A381-01': 'TCGA-2Z-A9J5-01A'},
 'survival': {'TCGA-2K-A9WE-01A-11D-A381-01': 'lts',
  'TCGA-2Z-A9J1-01A-11D-A381-01': 'lts',
  'TCGA-2Z-A9J2-01A-11D-A381-01': 'lts',
  'TCGA-2Z-A9J3-01A-12D-A381-01': 'non-lts',
  'TCGA-2Z-A9J5-01A-21D-A381-01': 'lts'}}
0 Answers
Related