如何显示
df['column_name'].value_counts()
的(流式)条形图?
使用 Seaborn 和 st.pyplot
import seaborn as sns
import streamlit as st
val_count = df['column_name'].value_counts()
fig = plt.figure(figsize=(10,5))
sns.barplot(val_count.index, val_count.values, alpha=0.8)
fig.title('Some title')
fig.ylabel('y label', fontsize=12)
fig.xlabel('x label', fontsize=12)
# Add figure in streamlit app
st.pyplot(fig)
或
将 pandas
value_counts
输出转换为 dataframe
df1 = df['column_name'].value_counts().rename_axis('unique_values').reset_index(name='counts')
st.bar_chart(df1)
import streamlit as st
import pandas as pd
# Sample DataFrame
data = {'column_name': ['A', 'B', 'A', 'C', 'B', 'A', 'D']}
df = pd.DataFrame(data)
# Get value counts
distribution = df['column_name'].value_counts()
# Display bar chart
st.bar_chart(distribution)