Calmcode - streamlit: pandas

How to show Pandas Dataframes in streamlit.

1 2 3 4 5 6 7 8 9

This is what the dataset looks like in our video:

|       date | currency_code | name         | local_price |  dollar_ex | dollar_price |
| ---------- | ------------- | ------------ | ----------- | ---------- | ------------ |
| 2000-04-01 | ARS           | Argentina    |      2.500… |     1.000… |       2.500… |
| 2000-04-01 | AUD           | Australia    |      2.590… |     1.680… |       1.542… |
| 2000-04-01 | BRL           | Brazil       |      2.950… |     1.790… |       1.648… |

This is the app.py that we end up with at the end of the video;

import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pylab as plt

st.title('BigMac Index')

@st.cache
def load_data():
    data = pd.read_csv("bigmac.csv")
    return data.assign(date = lambda d: pd.to_datetime(d['date']))

df = load_data()

countries = st.sidebar.multiselect(
    "Select Countries",
    df['name'].unique()
)

varname = st.sidebar.selectbox(
    "Select Column",
    ("local_price", "dollar_price")
)

subset_df = df.loc[lambda d: d['name'].isin(countries)]

for name in countries:
    plotset = subset_df.loc[lambda d: d['name'] == name]
    plt.plot(plotset['date'], plotset[varname], label=name)
plt.legend()
st.pyplot()

if st.sidebar.checkbox("Show Raw Data"):
    st.markdown("### Raw Data")
    st.write(subset_df)

Note that we run this app from the command line by running;

streamlit run app.py