World Population from 1955 to 2020 Bar Chart Race | by Durgesh Samariya | Towards Data Science

Welcome back to my 100 Days of Data Science Challenge Journey. On day 6, I worked on Bar Chart Race Animation using Python. For today’s task, I will be using the Countries Population from 1955 to 2020 dataset available on Kaggle.
You can read my previous days stories here:
towardsdatascience.com
Recently, I come across a medium post on announcement off Official Release of bar_chart_race by Ted Petrou. In his article, he provides an excellent tutorial on how to create Bar Chart Race using bar_chart_race package. Check out the official document here.
Installation of Bar Chart Race package
Import required libraries
Loading Data
Creating Bar Chart Race
Install with pip:
pip3 install bar_chart_race
or using anaconda:
conda install -c conda-forge bar_chart_race
For this project, we only need Pandas, Bar_Chart_Race libraries.
# to load dataimport pandas as pd#bar chart raceimport bar_chart_race as bcr
%time population = pd.read_csv('./data/Countries Population from 1995 to 2020.csv')
In bar chart race, your data must be in a specific format:
each entry represents a single time
each feature have some single particular value
time should be set as .index
Let’s have a look at how our data is looking.
population.head()
So it’s clear that our data is not in the appropriate format to feed in bar_chart_race. First, make relevant changes in data.
Step 1 — Remove all columns except Year, Country, and Population.
pop = population.drop(['Yearly % Change', 'Yearly Change', 'Migrants (net)', 'Median Age', 'Fertility Rate', 'Density (P/Km2)', 'Urban Pop %', 'Urban Population', 'Country\'s Share of World Pop %', 'World Population', 'Country Global Rank' ], axis = 1)
Step 2 — Create pivot_table from the pop data frame where Year is an index; each country as column and Population as value.
df = pop.pivot_table('Population',['Year'], 'Country')
Sometimes your data is not in order, so make sure you order the time column. In our case, its Year.
df.sort_values(list(df.columns),inplace=True)df = df.sort_index()df
Now our data is ready, so let’s create a bar chart race.
You can simply use .bar_chart_race()method from bcr.
bcr.bar_chart_race(df)
The above give step is very simple and not as attractive as I want. So let’s customize it. Let’s have a look at the final code. You can find all features and different possibilities in bar_chart_plot documentation.
bcr.bar_chart_race(    df=df,    filename=None,    orientation='h',    sort='desc',    n_bars=10,    fixed_order=False,    fixed_max=True,    steps_per_period=10,    interpolate_period=False,    label_bars=True,    bar_size=.90,    period_label={'x': .99, 'y': .25, 'ha': 'right', 'va':'center'},    period_summary_func=lambda v, r: {'x': .99, 'y': .18,             's': f'Population{v.nlargest(39).sum():,.0f}',             'ha': 'right', 'size': 8, 'family': 'Courier New'},    period_length=500,    figsize=(6.5,5),    dpi=144,    cmap='dark12',    title='Population by Country',    title_size='',    bar_label_size=7,    tick_label_size=5,    shared_fontdict={'family' : 'Helvetica','color' : '.1'},    scale='linear',    writer=None,    fig=None,    bar_kwargs={'alpha': .7},    filter_column_colors=True)
I would like to thanks Ted Petrou for creating such a great library and Tutorials. Apart from that, thanks to Vinay Shaw — author of this kernel, who introduced me to this beautiful library.
That’s it for today. Today, I learned and created my first bar chart race visualization. In future looking forward to create something interesting from it. I hope this article was helpful to you. All the codes of data analysis and visuals can be found at this GitHub repository or Kaggle kernel.
Thanks for reading.
I appreciate any feedback.
towardsdatascience.com
If you like my work and want to support me, I’d greatly appreciate it if you follow me on my social media channels:
The best way to support me is by following me on Medium.
Subscribe to my new YouTube channel.
Sign up on my email list.