Creating a leaderboard in streamlit.

huangapple go评论62阅读模式
英文:

Creating a leaderboard in streamlit

问题

I'm new to Python and Streamlit and I'm trying to create a leaderboard.
我是Python和Streamlit的新手,我正在尝试创建一个排行榜。

The dataframe looks something like this.
数据框看起来像这样。

So the idea is to create a graph that ranks their best results from the selected metric.
因此,想法是创建一个图表,根据选择的指标对其最佳结果进行排名。

I visualize something like this
我想象的是这样的

This is the code I got so far
这是我到目前为止得到的代码

df=pd.read_csv('forcedecks-test-export-04_05_2023.csv', parse_dates=["Date"])

sb_options = list(df.columns)

colselect1,colselect2 = st.columns(2)
with colselect1:
metric=st.selectbox('Select the Metric', options=sb_options)

plot_assym = px.bar(df, x=metric.max(), y=df['Name'],template="plotly_white")

I appreciate the help!我感谢您的帮助!

英文:

I'm new to Python and Streamlit and I'm trying to create a leaderboard.
The dataframe looks something like this.

Creating a leaderboard in streamlit.
So the idea is to create a graph that ranks their best results from the selected metric.
I visualize something like this
Creating a leaderboard in streamlit.

This is the code I got so far

df=pd.read_csv('forcedecks-test-export-04_05_2023.csv', parse_dates=["Date"])

sb_options = list(df.columns)

colselect1,colselect2 = st.columns(2)
with colselect1:
   metric=st.selectbox('Select the Metric', options=sb_options)
      
plot_assym = px.bar(df, x=metric.max(), y=df['Name'],template="plotly_white")

I appreciate the help!

答案1

得分: 1

以下是代码的翻译部分:

import streamlit as st
import pandas as pd
import plotly.express as px

data = {
    'Name': ['Paul', 'Mark', 'Will', 'Ben'],
    'Metric 1': [25, 96, 12, 12],
    'Metric 2': [10, 8, 5, 8],
}

df = pd.DataFrame(data)

st.dataframe(df)

sb_options = list(df.columns)
sb_options.remove('Name')

colselect1, colselect2 = st.columns(2)
with colselect1:
   sel_metric = st.selectbox('Select the Metric', options=sb_options)

# 创建一个新的用于绘图的数据框。按指标值排序以便于绘图显示。
# 如果指标值相同,按字母顺序显示名称。
df_metric = df.copy()
df_metric = df_metric.sort_values(by=[sel_metric, 'Name'], ascending=[True, False])

plot_assym = px.bar(df_metric, x=sel_metric, y="Name", template="plotly_white", text_auto=True)
st.plotly_chart(plot_assym)

希望这有所帮助。

英文:

Try this, there are comments in the code.

import streamlit as st
import pandas as pd
import plotly.express as px

data = {
    'Name': ['Paul', 'Mark', 'Will', 'Ben'] ,
    'Metric 1': [25, 96, 12, 12],
    'Metric 2': [10, 8, 5, 8],
}

df = pd.DataFrame(data)

st.dataframe(df)

sb_options = list(df.columns)
sb_options.remove('Name')

colselect1,colselect2 = st.columns(2)
with colselect1:
   sel_metric = st.selectbox('Select the Metric', options=sb_options)

# Create a new dataframe for plotting. Sort it for plotting display.
# If metric value is the same, display name by alphabetic order.
df_metric = df.copy()
df_metric = df_metric.sort_values(by=[sel_metric, 'Name'], ascending=[True, False])
      
plot_assym = px.bar(df_metric, x=sel_metric, y="Name", template="plotly_white", text_auto=True)
st.plotly_chart(plot_assym)

Output

Creating a leaderboard in streamlit.

huangapple
  • 本文由 发表于 2023年5月18日 01:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定