It is a code that tries to graph the current death rate by Corona country. In this code, the for statement shows all the names of the countries on the page, the r_rate, and the country-specific mortality on the page, but outside the for statement, I tried to use the name and r_rate to graph them, but only the last country and the last country. Is there anyone who knows how to make every country and every country show mortality?
import urllib.request
import requests
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
from urllib import parse
import matplotlib.pyplot as plt
url='https://coronavirus.jhu.edu/data/mortality'
fp = urllib.request.urlopen(url)
source = fp.read()
fp.close()
soup = BeautifulSoup(source, "html.parser")
base_info= soup.find("div", {"class": "TFormat_main__35Moj"})
second_info=base_info.find_all('td')
for i in range(0,846,5):
name=second_info[i].text.strip()
for i in range(3,849,5):
rate=second_info[i].text.strip()
r_rate=float(rate.replace("%",""))
plt.rc('font', family='Malgun Gothic')
country=[name]
drate=[r_rate]
plt.rc('font', family='Malgun Gothic')
plt.figure(figsize=(10,6))
Colors=['red','green','blue','black']
plt.bar(country,drate,color=Colors)
plt.title('Covid19 Fatality Rate 2020.12')
plt.ylabel('Fatality Rate (%)')
plt.grid()
plt.show()
print(country)
print(drate)
Try doing something. There's only one element.
It's a question.
If you can answer the questions above, you can solve them.
Additionally, there is a very good function called pd.read_html
.
dfs = pd.read_html('https://coronavirus.jhu.edu/data/mortality')
df = dfs[0]
df['Death Rate'] = df['Deaths']/df['Confirmed']
plt.bar(df['Country'], df['Death Rate'])
plt.show()
df1 = df[:10]
plt.bar(df1['Country'], df1['Death Rate'])
plt.show()
Do this, too.
© 2024 OneMinuteCode. All rights reserved.