1

To be honest, I'm new to Pandas and to Streamlit, however, I tried to do some stuff for my personal interest.

Here's the script so far:

import pandas as pd
import streamlit as st

st.title('Vaccine Overview')

@st.cache_data
def get_data():
    df = pd.read_csv('https://opendata.sozialversicherung.at/eimpfpass/COVID19_vaccination_agegroups_v202210.csv', sep=';')
    return df

df = get_data()

df_age_vacc = df.query('state_id == 11')\
    [['age_group','vaccine','vaccinations_administered_cumulative']]\
        .groupby(['age_group','vaccine'])['vaccinations_administered_cumulative']\
            .sum()

df_age_vacc.index.set_names(['Altersgruppe','Impfstoff'], inplace=True)
df_age_vacc.rename('Menge', inplace=True)

st.bar_chart(df_age_vacc)

Whenever I run the script using Streamlit I get the following error: KeyError: "The following 'id_vars' are not present in the DataFrame: ['index']"

What am I doing wrong here? As I noted I'm an absolute beginner with this, Pandas is not my domain.

Any help is very appreciated. Thanks.

In [70]: df_age_vacc.index
Out[70]: 
MultiIndex([(      '00-11',    'AstraZeneca'),
            (      '00-11', 'BioNTechPfizer'),
            (      '00-11',        'Janssen'),
            (      '00-11',        'Moderna'),
            (      '00-11',        'Novavax'),
            (      '00-11', 'Sanofi Pasteur'),
            (      '00-11',        'Valneva'),
            (      '12-14',    'AstraZeneca'),
            (      '12-14', 'BioNTechPfizer'),
            (      '12-14',        'Janssen'),
            (      '12-14',        'Moderna'),
            (      '12-14',        'Novavax'),
            (      '12-14', 'Sanofi Pasteur'),
            (      '12-14',        'Valneva'),
            (      '15-24',    'AstraZeneca'),
            (      '15-24', 'BioNTechPfizer'),
            (      '15-24',        'Janssen'),
            (      '15-24',        'Moderna'),
            (      '15-24',        'Novavax'),
            (      '15-24', 'Sanofi Pasteur'),
            (      '15-24',        'Valneva'),
            (      '25-34',    'AstraZeneca'),
            (      '25-34', 'BioNTechPfizer'),
            (      '25-34',        'Janssen'),
            (      '25-34',        'Moderna'),
            (      '25-34',        'Novavax'),
            (      '25-34', 'Sanofi Pasteur'),
            (      '25-34',        'Valneva'),
            (      '35-44',    'AstraZeneca'),
            (      '35-44', 'BioNTechPfizer'),
            (      '35-44',        'Janssen'),
            (      '35-44',        'Moderna'),
            (      '35-44',        'Novavax'),
            (      '35-44', 'Sanofi Pasteur'),
            (      '35-44',        'Valneva'),
            (      '45-54',    'AstraZeneca'),
            (      '45-54', 'BioNTechPfizer'),
            (      '45-54',        'Janssen'),
            (      '45-54',        'Moderna'),
            (      '45-54',        'Novavax'),
            (      '45-54', 'Sanofi Pasteur'),
            (      '45-54',        'Valneva'),
            (      '55-64',    'AstraZeneca'),
            (      '55-64', 'BioNTechPfizer'),
            (      '55-64',        'Janssen'),
            (      '55-64',        'Moderna'),
            (      '55-64',        'Novavax'),
            (      '55-64', 'Sanofi Pasteur'),
            (      '55-64',        'Valneva'),
            (      '65-74',    'AstraZeneca'),
            (      '65-74', 'BioNTechPfizer'),
            (      '65-74',        'Janssen'),
            (      '65-74',        'Moderna'),
            (      '65-74',        'Novavax'),
            (      '65-74', 'Sanofi Pasteur'),
            (      '65-74',        'Valneva'),
            (      '75-84',    'AstraZeneca'),
            (      '75-84', 'BioNTechPfizer'),
            (      '75-84',        'Janssen'),
            (      '75-84',        'Moderna'),
            (      '75-84',        'Novavax'),
            (      '75-84', 'Sanofi Pasteur'),
            (      '75-84',        'Valneva'),
            (        '85+',    'AstraZeneca'),
            (        '85+', 'BioNTechPfizer'),
            (        '85+',        'Janssen'),
            (        '85+',        'Moderna'),
            (        '85+',        'Novavax'),
            (        '85+', 'Sanofi Pasteur'),
            (        '85+',        'Valneva'),
            ('NotAssigned',    'AstraZeneca'),
            ('NotAssigned', 'BioNTechPfizer'),
            ('NotAssigned',        'Janssen'),
            ('NotAssigned',        'Moderna'),
            ('NotAssigned',        'Novavax'),
            ('NotAssigned', 'Sanofi Pasteur'),
            ('NotAssigned',        'Valneva')],
           names=['Altersgruppe', 'Impfstoff'])

In [71]: type(df_age_vacc)
Out[71]: pandas.core.series.Series
brillenheini
  • 793
  • 7
  • 22
  • @jezrael so you have no problem answering `df_age_vacc.reset_index()` (at the time you don't feel that it's a duplicate of [this](https://stackoverflow.com/questions/20461165/how-to-convert-index-of-a-pandas-dataframe-into-a-column)), but as soon as I give an answer (that is not straightforward, it requires to know how bar_chart is working internally, which I explained in the answer), you find a dummy duplicate that has nothing to do with streamlit, that's blatant bad faith… – mozway Mar 21 '23 at 09:44
  • @mozway - `so you have no problem answering df_age_vacc.reset_index()` - yes, you are right, it should be closed if it will be correct answer. What I dont know in time of written answer. – jezrael Mar 21 '23 at 09:50
  • Then why answering it in the first place? But I bet that if yours had been the correct answer you wouldn't have closed it ;) – mozway Mar 21 '23 at 09:54

1 Answers1

2

You might need to unstack your data as bar_chart is using the index as x and the columns as y and internally melts the data:

st.bar_chart(df_age_vacc.unstack())

Output:

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75