Home > Net >  Why can I not add a column in csv file to dataframe.query() with streamlit library?
Why can I not add a column in csv file to dataframe.query() with streamlit library?

Time:04-17

I am new to python and have historically used php, but I am working on building a simple dashboard in python mainly using pandas and streamlit just to get more familiar with the language, but am hung up on something that feels so simple. The web app created with streamlit filters down on "route" and "productgroup" perfectly fine, but as soon as I add the variable "user" looking at column "store user" in my data feed, I am met with a traceback error. I am assuming it is because of the space that is between "store" and "user", but the data source I am pulling from has that space in the column and I have not been able to find any documentation on dealing with a column name containing a space.

Any insight is appreciated!

Here are the libraries I have imported -

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

and here is the sidebar I have created so far

st.sidebar.header("Please Filter Here:")
route = st.sidebar.multiselect(
    "Filter by Route:",
    options=df["route"].unique(),
    default=df["route"].unique()
)


productgroup = st.sidebar.multiselect(
    "Filter by Product Group:",
    options=df["productgroup"].unique(),
    default=df["productgroup"].unique()
)

user = st.sidebar.multiselect(
    "Filter by Store:",
    options=df["store user"].unique(),
    default=df["store user"].unique()
)

df_selection = df.query(
    "(route == @route) and (productgroup == @productgroup) and (user == @store user)" 
    ) 

st.dataframe(df_selection)

CodePudding user response:

The column name with space must be inside the backtick. See pandas ref.

Use the following.

(`store user` == @user)"

or can be

(@user == `store user`)"

store user is the column name while user is the variable. It is the variable that has to be preceded with @.

  • Related