对指标的所有百分比值列进行求和/平均

问题描述 投票:0回答:1

我有一个 pandas 数据框,如下所示:

enter image description here

我想在简化的指标 UI 中显示总计。我如何获得所有百分比值的总和/平均值

我尝试了下面的代码:

#compute top analytics
    total_investment = df['Investment']
    
    total1,total2,total3,total4,total5=st.columns(5,gap='small')
    with total1:
        st.info('Sum Investment',icon="💰")
        st.metric(label="Sum TZS",value=f"{total_investment}")

下面是我得到的结果 enter image description here

python pandas streamlit
1个回答
0
投票
 import pandas as pd
 import streamlit as st
   data = {
        'SA': ['47.96%', '46.30%', '65.19%', '54.26%', '90.74%', '70.16%']
    }
    df = pd.DataFrame(data)
    df['SA'] = df['SA'].str.rstrip('%').astype(float)
    sum = df['SA'].sum()
    avg = df['SA'].mean()
    st.metrics(
       label="sum",
       value=f"{sum:.2f}%",
)
    st.metrics(
       label="avg",
       value=f"{avg:.2f}%",
)

使用此代码并尝试一次

© www.soinside.com 2019 - 2024. All rights reserved.