I want to plot a interactive bar plot using plotly.express
state_count = df['state'].value_counts().head(10)
state_names = df['state'].value_counts().head(10).index
Bar_plot = px.bar(df,state_names,state_count, text='1',
title='Top 10 states that have the most haunted places',
labels={"y": "Count", "x": "States"})
Bar_plot.update_traces(textfont_size=12, textposition='outside', cliponaxis=False)
Bar_plot.show()
But I get an
Keyerror: 'count'
I tried to traceback the error but unable to find any error and I tried to add .reset_index()
but it generates a new error
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
The error "KeyError: 'count'" occurs because you are trying to access a column named "count" in your DataFrame df
, but there is no column with that name. The column name should be "state_count".
The error "ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." occurs because you are trying to use the value_counts()
method to get the count of unique values in the "state" column and then trying to use the result as a boolean value. This is not possible because the result of value_counts()
is a Series object, which is not a boolean value.
To fix these errors, you can do the following:
You can try this:
import pandas as pd
import plotly.express as px
# Create a DataFrame
df = pd.DataFrame({'state': ['California', 'New York', 'Texas', 'Florida', 'Illinois', 'Pennsylvania', 'Ohio', 'Georgia', 'North Carolina', 'Michigan']})
# Get the count of unique values in the "state" column
state_counts = df['state'].value_counts().tolist()
# Get the state names
state_names = df['state'].value_counts().index.tolist()
# Create the bar plot
Bar_plot = px.bar(df, state_names, state_counts, text='1',
title='Top 10 states that have the most haunted places',
labels={"y": "Count", "x": "States"})
# Update the plot appearance
Bar_plot.update_traces(textfont_size=12, textposition='outside', cliponaxis=False)
# Show the plot
Bar_plot.show()