Error: StreamlitAPIException: ("Expected bytes, got a 'int' object", 'Conversion failed for column value with type object')
Traceback: File "/Users/temporaryadmin/Streamlit_app_v.1/streamlit_app_v1/main.py", line 48, in <module> st.line_chart(df)
Problem:
I'm quite new to this and got quite excited with this first Streamlit project off mine. The file runs fine with I don't include the pandas_data = {'County': county_names}
but the list of strings stored in "county_names" is quite crucial to representing the data in the graph.
Any help in what to do would be highly appreciated, thank you. enter code here
Here is my code:
import streamlit as st
import pandas as pd
import requests
bas_url = 'https://services1.arcgis.com/eNO7HHeQ3rUcBllm/arcgis/rest/services/Covid19CountyStatisticsHPSCIrelandOpenData/FeatureServer/0/query?where=1=1&outFields=CountyName,PopulationCensus16,ConfirmedCovidCases,PopulationProportionCovidCases&outSR=4326&f=json'
response = requests.get(bas_url)
print(response)
get_json_response = requests.get(bas_url).json()
data = []
for i in get_json_response['features']:
data.append(i)
# List comprehension
new_data = [i['attributes'] for i in data]
# All variables below containing lists of data from api request
county_names = [i['CountyName'] for i in new_data]
population_total_2016 = [i['PopulationCensus16'] for i in new_data]
confirmed_cases = [i['ConfirmedCovidCases'] for i in new_data]
pandas_data = {
'County': county_names,
'Population': population_total_2016,
'Confirmed cases': confirmed_cases
}
df = pd.DataFrame(pandas_data)
st.write(df)
st.line_chart(df)
CodePudding user response:
You can make a bar chart with counties on the x axis by reshaping your data like this
import streamlit as st
import pandas as pd
import requests
bas_url = 'https://services1.arcgis.com/eNO7HHeQ3rUcBllm/arcgis/rest/services/Covid19CountyStatisticsHPSCIrelandOpenData/FeatureServer/0/query?where=1=1&outFields=CountyName,PopulationCensus16,ConfirmedCovidCases,PopulationProportionCovidCases&outSR=4326&f=json'
response = requests.get(bas_url)
print(response)
get_json_response = requests.get(bas_url).json()
data = []
for i in get_json_response['features']:
data.append(i)
# List comprehension
new_data = [i['attributes'] for i in data]
# All variables below containing lists of data from api request
county_names = [str(i['CountyName']) for i in new_data]
population_total_2016 = [i['PopulationCensus16'] for i in new_data]
confirmed_cases = [i['ConfirmedCovidCases'] for i in new_data]
pandas_data = {
'County': county_names,
'Population': population_total_2016,
'Confirmed cases': confirmed_cases
}
df = pd.DataFrame(pandas_data)
df = df.groupby('County')[['Population', 'Confirmed cases']].sum()
st.write(df)
st.bar_chart(df)
Grouping the dataframe and making the counties the dataframe's index will tell streamlit
to use them as your x axis and then will use the values in Population
and Confirmed cases
to make the bars.