python 如何让Streamlit在没有逗号的数据框中显示年份?

xtfmy6hx  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(98)

我正在为学校的期末项目创建Streamlit应用程序。它包含两个原始 Dataframe 和两个图形。然而,当我将数据框发布到应用程序时,Year列出现逗号,即。1,993年,而不是1,993年。
到目前为止,我已经尝试将Year列设置为int和对象来保存清理后的数据,但没有成功。我还尝试将清理后的数据保存为.csv而不是.xlsx,以加载到Streamlit代码中,以防Excel格式中出现一些奇怪的东西,导致逗号出现-这也不起作用。我希望 Dataframe 以YYYY格式发布到Streamlit应用程序,而不是Y,YYY格式,但我得到了Y,YYY格式。最后,我使用matplotlib来发布图表,因为它没有添加不必要的逗号。
这就是我的streamlit代码的样子:

import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st

st.title('Global Biodiversity Decline')

st.write(' ')
st.write(' ')
st.write(' ')

live=pd.read_excel('living-planet-spread.xlsx')

live=live.drop(axis=1, columns='Unnamed: 0')

live['Year']=live['Year'].astype('object')

live2=pd.pivot_table(live, index='Year', columns='Region', values='Average Index', fill_value=0)

st.subheader('Decline of Average Index by Year')

if st.checkbox('Show Raw Biodiversity Data'):
    st.subheader('Raw Data')
    st.write(live2)
    st.caption("Data Source: World Wildlife Fund (WWF) and Zoological Society of London")

chart=pd.DataFrame(live2, columns=['Africa', 'Asia and Pacific', 'Europe and Central Asia', 'Latin America and the Carribean', 'North America', 'World'])

fig, ax=plt.subplots(figsize=(12,6))
ax.plot(chart)
ax.set(xlabel='Year', ylabel='Index (%)')
ax.legend(['Africa', 'Asia', 'Europe', 'South America', 'North America'])
st.pyplot(fig)

st.caption('Above is a graph plotting the average index of biodiversity per region. Note that all regions are on a steady decline, particularly Latin America which has a sharper decline than all other regions. One possible cause of this could be deforestation related to farming. See the below graph.')

st.write(' ')
st.write(' ')
st.write(' ')

#I had to set the index as 'Year' in order for the x-axis of this graph to show up as the Years instead of a numbered index
land=pd.read_excel('fao_land_data_spread.xlsx')
land=land.set_index('Year')

st.subheader('Regional Increase in Land Use for Farming by Year')

if st.checkbox('Show Raw Land Area Data'):
    st.subheader('Raw Data')
    st.write(land)
    st.caption('Data Source: UNData')

chart2=pd.DataFrame(land, columns=['Africa', 'Asia', 'Europe', 'South America', 'North America'])
chart3=pd.DataFrame(land, columns=['World'])

fig, ax=plt.subplots(figsize=(12,6))
ax.plot(chart2)
ax.set(xlabel='Year', ylabel='Area (1000 Ha)e+06')
ax.legend(['Africa', 'Asia', 'Europe', 'South America', 'North America'])
st.pyplot(fig)

st.caption('Above is a graph plotting the area of farmland used per region...')

st.write(' ')
st.write(' ')
st.write(' ')

st.subheader('Global Increase in Land Use for Farming by Year')

fig, ax=plt.subplots(figsize=(12,6))
ax.plot(chart3)
ax.set(xlabel='Year', ylabel='Area (1000 Ha)e+06')
st.pyplot(fig)

st.caption('I put the Global area of farmland in its own graph...')

这是每个 Dataframe 的示例:

Africa  Asia    Europe  North America   South America   World
Year                        
1961    927526.222222   911930.555556   825966.444444   586216.444444   502466.333333   4.146173e+06
1962    927657.000000   913559.333333   826292.888889   585067.666667   503954.444444   4.149369e+06
1963    928080.888889   914962.222222   825754.111111   584786.000000   505403.444444   4.152637e+06
1964    928313.333333   916675.333333   825170.777778   584079.000000   506533.333333   4.155457e+06
1965    928717.111111   918125.555556   825569.555556   583276.444444   507664.888889   4.159057e+06
Region   Year   Average Index   Upper Index Lower Index
44  Africa  2014    32.492869   68.628636   15.238575
45  Africa  2015    31.293573   66.256152   14.669147
46  Africa  2016    32.054221   68.026893   14.968882
47  Africa  2017    34.445875   73.433580   15.991854
48  Africa  2018    34.445875   73.433580   15.991854
svmlkihl

svmlkihl1#

从您的描述和代码片段来看,逗号似乎是由Year列作为数值类型从Excel中读入引起的。逗号似乎是在将numeric type转换为object type时引入的,这似乎是Pandas excel reader的默认行为。
您可以尝试将Year的数据类型指定为String,然后将其转换回numeric或int:

live=pd.read_excel('living-planet-spread.xlsx', dtype={'Year': str})

# Convert the "Year" column to a numeric 
live['Year'] = pd.to_numeric(live['Year'])

# Convert the "Year" column to an integer
live['Year'] = live['Year'].astype(int)

相关问题