0

The code below works fine

data_2015=(data[data.Year==2015]
    .groupby("Country")
    ["Country", "Life expectancy "]
    .median()
    .sort_values(by="Life expectancy ", ascending=True))

data_2015.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2000, height=550, title = "Life Expectancy for ALL Countries for 2015")

but when I try to input data_year dynamically with the code below, the plot does not show

year = input ('Life expectancy ranking of what year between 2000-2015 are you interested in?:  ')

data_year=(data[data.Year==year]
   .groupby(["Country"])
    [["Country", "Life expectancy "]]
    .median()
    .sort_values(by="Life expectancy ", ascending=False))

data_year.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2100, height=500, title ="Life expectancy ranking of countries in 2015")

what am i missing?

  • What error are you getting? Can you pls share some sample data – Redox Sep 10 '22 at 10:16
  • @Redox thanks for your comment. I did not get any errors. The plot did not just display. This is the link to the dataset: (https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who?select=Life+Expectancy+Data.csv –  Sep 11 '22 at 09:12
  • Pls check answer and see if works – Redox Sep 11 '22 at 09:51

1 Answers1

0

The issue you are facing is due to the input process. When you use input(), the data is read as a string. Convert it to integer using int() and it will work.

Updated code

year = input ('Life expectancy ranking of what year between 2000-2015 are you interested in?:  ')

data_year=(data[data.Year==int(year)]
   .groupby(["Country"])
    [["Country", "Life expectancy "]]
    .median()
    .sort_values(by="Life expectancy ", ascending=False))

myTitle = "Life expectancy ranking of countries in " + year
data_year.reset_index().hvplot.bar(x="Country", y="Life expectancy ", rot=90,width=2100, height=500, title = myTitle)

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • thank you so much. I now understand why. one more question please, is there a way to capture the input year in the plot title? –  Sep 11 '22 at 10:14
  • Just create a string and use that when you write the title. Updated code in response – Redox Sep 11 '22 at 10:22