+1 vote
in Python by
You have this covid-19 dataset below:

From this dataset, how will you make a bar-plot for the top 5 states having maximum confirmed cases as of 17=07-2020?

sol:

#keeping only required columns

df = df[[‘Date’, ‘State/UnionTerritory’,’Cured’,’Deaths’,’Confirmed’]]

#renaming column names

df.columns = [‘date’, ‘state’,’cured’,’deaths’,’confirmed’]

#current date

today = df[df.date == ‘2020-07-17’]

#Sorting data w.r.t number of confirmed cases

max_confirmed_cases=today.sort_values(by=”confirmed”,ascending=False)

max_confirmed_cases

#Getting states with maximum number of confirmed cases

top_states_confirmed=max_confirmed_cases[0:5]

#Making bar-plot for states with top confirmed cases

sns.set(rc={‘figure.figsize’:(15,10)})

sns.barplot(x=”state”,y=”confirmed”,data=top_states_confirmed,hue=”state”)

plt.show()

1 Answer

0 votes
by

Below is the explanation of the above code set of test

Code explanation:

We start off by taking only the required columns with this command:

df = df[[‘Date’, ‘State/UnionTerritory’,’Cured’,’Deaths’,’Confirmed’]]

Then, we go ahead and rename the columns:

df.columns = [‘date’, ‘state’,’cured’,’deaths’,’confirmed’]

After that, we extract only those records, where the date is equal to 17th July:

today = df[df.date == ‘2020-07-17’]

Then, we go ahead and select the top 5 states with maximum no. of covide cases:

max_confirmed_cases=today.sort_values(by=”confirmed”,ascending=False)

max_confirmed_cases

top_states_confirmed=max_confirmed_cases[0:5]

Finally, we go ahead and make a bar-plot with this:

sns.set(rc={‘figure.figsize’:(15,10)})

sns.barplot(x=”state”,y=”confirmed”,data=top_states_confirmed,hue=”state”)

plt.show()

Here, we are using seaborn library to make the bar-plot. “State” column is mapped onto the x-axis and “confirmed” column is mapped onto the y-axis. The color of the bars is being determined by the “state” column.

Related questions

+1 vote
asked Feb 15, 2021 in Python by SakshiSharma
0 votes
asked Sep 29, 2021 in Python by john ganales
...