Hello Community,
Can anyone help me with this please. I want to create an OrderedDict from a csv file and use it to populate pages.
Hello @wilson_uzziel,
You can use pandas to load .csv
files, process data the way you want, and display the result by using st.write
.
Out of curiosity, why do you want to use OrderedDict
instead of regular python dict
?
Hello @okld,
You the reason i need to use OrderedDict is that it will enable population of pages based on selected results from selectbox. For example i have a csv file which contains GDP data from this data i want a page generated once the use choose a country from the selectbox. Then, thereafter filter data based on the choice
Is there a way of working out this without using OrderedDict. Please help
As of Python 3.6 (using CPython implementation), regular dicts keep the insertion order like OrderedDict
. And since 3.7, python specification enforced this behavior for all python implementations.
Thank @okld,
At the moment, am in desperate situation of working out this right. I sincerely request you help even if it means using python dict i will appreciate.
Hi @wilson_uzziel -
I do need to ask you to be patient with users on this message board; people are trying to help you, but we cannot guarantee that we can answer your question on a specific timeframe.
As @okld has indicated, the solution to do this is possible and unrelated to Streamlit. You will likely get a lot more help if you can give us a code snippet of what you’ve tried, and ask how to solve a particular issue. Telling us “I want to use OrderedDict” doesn’t give us much to work with.
Best,
Randy
As @randyzwitch said, without more info, we may not help you the way you want.
However, just to give you an example of what you can do, here is a generic code snippet to display a DataFrame, and filter columns. I used this dataset for my tests. You’ll notice that I didn’t use any dict or OrderedDict at all.
import pandas as pd
import streamlit as st
def main():
st.sidebar.title("GDP reader")
df = load_csv("gdp.csv")
for column in df.columns:
options = pd.Series(["All"]).append(df[column], ignore_index=True).unique()
choice = st.sidebar.selectbox("Select {}.".format(column), options)
if choice != "All":
df = df[df[column] == choice].drop(columns=column)
st.table(df)
@st.cache
def load_csv(path):
return pd.read_csv(path)
if __name__ == "__main__":
main()
@randyzwitch @okld, I am sorry for my impatience.
I like to say i have tried out the script though I am get an error on for column in df.columns:
@wilson_uzziel I’m sorry but I won’t be able to help with so few information. What error do you have? Have you tested my code with the dataset I linked or with yours?
Well, the error is self-explanatory. You have an indentation error: ensure you don’t have any additional space or tab at the beginning of this line.