9. Exploring the Gapminder Data set#
How to do it#
Import the
plotly.expressmodule aspx
import plotly.express as px
Load the data set and check its type
gapminder = px.data.gapminder()
type(gapminder)
pandas.core.frame.DataFrame
Inspect the
DataFrameusing the methodheadandtail
gapminder.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
gapminder.tail()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 1699 | Zimbabwe | Africa | 1987 | 62.351 | 9216418 | 706.157306 | ZWE | 716 |
| 1700 | Zimbabwe | Africa | 1992 | 60.377 | 10704340 | 693.420786 | ZWE | 716 |
| 1701 | Zimbabwe | Africa | 1997 | 46.809 | 11404948 | 792.449960 | ZWE | 716 |
| 1702 | Zimbabwe | Africa | 2002 | 39.989 | 11926563 | 672.038623 | ZWE | 716 |
| 1703 | Zimbabwe | Africa | 2007 | 43.487 | 12311143 | 469.709298 | ZWE | 716 |
Use the method
describe
gapminder.describe()
| year | lifeExp | pop | gdpPercap | iso_num | |
|---|---|---|---|---|---|
| count | 1704.00000 | 1704.000000 | 1.704000e+03 | 1704.000000 | 1704.000000 |
| mean | 1979.50000 | 59.474439 | 2.960121e+07 | 7215.327081 | 425.880282 |
| std | 17.26533 | 12.917107 | 1.061579e+08 | 9857.454543 | 248.305709 |
| min | 1952.00000 | 23.599000 | 6.001100e+04 | 241.165876 | 4.000000 |
| 25% | 1965.75000 | 48.198000 | 2.793664e+06 | 1202.060309 | 208.000000 |
| 50% | 1979.50000 | 60.712500 | 7.023596e+06 | 3531.846989 | 410.000000 |
| 75% | 1993.25000 | 70.845500 | 1.958522e+07 | 9325.462346 | 638.000000 |
| max | 2007.00000 | 82.603000 | 1.318683e+09 | 113523.132900 | 894.000000 |
Make a scatter plot to visualize the relationship between GDP per capita and Life Expectancy for the oldes and the most recent year (i.e. 1952 and 2007)
fig = px.scatter(gapminder[gapminder.year == 1952], x='gdpPercap', y='lifeExp')
fig.show()
fig = px.scatter(gapminder[gapminder.year == 2007], x='gdpPercap', y='lifeExp')
fig.show()
Enhance the scatter plot by adding color the dots according to the continent.
fig = px.scatter(gapminder[gapminder.year == 2007], x='gdpPercap', y='lifeExp', color='continent')
fig.show()