This week, we looked at power plants around the world, and specifically at nuclear power plants. My interest was piqued by a recent set of stories describing how Microsoft is investing in the re-activation of Three Mile Island's nuclear power plant, in order to generate enough electricity for OpenAI's data needs without generating too many carbon emissions, (More details are in this New York Times article: https://www.nytimes.com/2024/09/20/climate/three-mile-island-reopening.html?unlocked_article_code=1.Qk4.saFR.wp4WwwvU0CYJ&smid=url-share)
Moreover, this gave us a chance to work a bit more with GeoPandas – creating a GeoDataFrame from non-geographical data, and then creating interactive maps with information about nuclear power plants.
Data set and six questions
This week's data set comes from the World Resources Institute (https://wri.org). Specifically, the data comes from their Global Power Plant database:
https://datasets.wri.org/datasets/global-power-plant-database
You can use that Web page to explore the map of power plants around the world. But of course, we're looking to download the data and analyze it. On the main database page, make sure that the "data files" tab is selected. There are several versions of the database schema, as well as of the data itself. We're looking for version 1.3.0, which is at the end of the list. As of this writing, the data was last updated on September 25th. Click on the "download" button, which will download a zipfile from Amazon S3. That file, when opened, includes a CSV file that we'll use for our data.
Load the data into a GeoDataFrame, using the longitude and latitude columns as inputs for a Point in the geometry column.
For starters, we'll need to load some libraries. This week, because we'll be using not only Pandas, but also GeoPandas, we'll need to load both of them.
import pandas as pd
import geopandas as gpdNow let's load the data from the downloaded CSV file into a Pandas data frame using read_csv. The only unusual thing here is that I passed the low_memory=False keyword argument. This tells Pandas that there's enough memory to read the entire file into memory, thus allowing it to analyze the contents of each column and decide what dtype it should assign. Without this keyword argument, read_csv complained to me that it had found competing dtypes in some of the columns, because it reading the data into memory in smaller chunks, and choosing a dtype for each chunk:
filename = 'global_power_plant_database.csv'
df = pd.read_csv(filename, low_memory=False)
For most queries against this data set, that would be enough. But I asked you to turn it into a GeoDataFrame. A GeoDataFrame is just like a regular DataFrame object, except that it has a geometry column containing the geometric data we want to work with. If we call a regular Pandas method, then a GeoDataFrame works just like a DataFrame. But if we call a special GeoPandas method, then it uses the data in the geometry column to perform its calculations.
We can create a GeoDataframe based on an existing data frame, but we need to supply a geometry column. How can we do that?
One way would be to create a list of Point values, using the shapely library. We could iterate over the longitude and latitude columns in df, creating a Point for each one, and using the resulting list to create our GeoDataFrame.
But GeoPandas comes with a convenience method, gpd.points_from_xy, which we can use to create our geometry column more easily and efficiently:
gdf = gpd.GeoDataFrame(
df,
geometry=gpd.points_from_xy(df.longitude, df.latitude),
crs="EPSG:4326"
)
The result of this is that gdf is now a GeoDataFrame containing the same information as df did, but with an additional geometry column containing Point values, the location of each power plant.
It's important to remember, though, that geographic data can be described in a variety of ways, using a number of different coordinate systems. It's thus not enough for us to indicate the geometry column. We also need to indicate what "coordinate reference system," or CRS, we plan to use. In this case, because we got data in real-world longitude and latitude, we can use "EPSG:4326", the designation for such coordinates.
The result is a GeoDataFrame containing 34,936 rows and 37 columns. (This is one more column than df, thanks to the geometry column.) If we run gdf.dtypes, to get the dtype of each column, we find:
country object
country_long object
name object
gppd_idnr object
capacity_mw float64
latitude float64
longitude float64
primary_fuel object
other_fuel1 object
other_fuel2 object
other_fuel3 object
commissioning_year float64
owner object
source object
url object
geolocation_source object
wepp_id object
year_of_capacity_data float64
generation_gwh_2013 float64
generation_gwh_2014 float64
generation_gwh_2015 float64
generation_gwh_2016 float64
generation_gwh_2017 float64
generation_gwh_2018 float64
generation_gwh_2019 float64
generation_data_source object
estimated_generation_gwh_2013 float64
estimated_generation_gwh_2014 float64
estimated_generation_gwh_2015 float64
estimated_generation_gwh_2016 float64
estimated_generation_gwh_2017 float64
estimated_generation_note_2013 object
estimated_generation_note_2014 object
estimated_generation_note_2015 object
estimated_generation_note_2016 object
estimated_generation_note_2017 object
geometry geometry
dtype: objectNotice that the final column, geometry, has a dtype of geometry, which is exactly what we wanted.
How many power plants are there in the world? Which 10 countries have the most plants? If we compare the mean and median number per country, what do we see, and what can we learn from that?
To find out how many power plants there are, we can use the count method on a column – say, on the country_long column, which contains the full country names:
gdf['country_long'].count()This returns 34,936, the number of non-NaN values in the series.
But beyond that, we want to know the number of plants per country. There are a few ways to do this, among them using groupby – but Pandas provides us with a more convenient function, value_counts, which gives us the number of times each value appears in country_long. The values are automatically sorted from most to least common, so we can get the top 10 with head:
(
gdf
['country_long'].value_counts()
.head(10)
)Here's what I get:
country_long
United States of America 9833
China 4235
United Kingdom 2751
Brazil 2360
France 2155
India 1589
Germany 1309
Canada 1159
Spain 829
Russia 545
Name: count, dtype: int64We see that the United States has more than twice as many power plants as China, which again has about twice as many as the UK. India, which has roughly the same population as China, has fewer power plants than France, which I wouldn't have expected.
Of course, this doesn't necessarily mean anything, because a country could have fewer, larger (i.e., generating more megawatt) plants rather than many, smaller plants.
If we want to compare the mean and median number of plants per country, we can use describe, which provides a full set of descriptive statistics:
(
gdf
['country_long'].value_counts()
.describe()
)The result:
count 167.000000
mean 209.197605
std 896.913269
min 1.000000
25% 7.000000
50% 16.000000
75% 60.500000
max 9833.000000
Name: count, dtype: float64Notice that the mean is 209 power plants, but the median is 16. The difference shows just how much a few outliers can skew the mean, potentially making us think that the "average" country has a very large number of power plants. We can see, though, that even countries in the 75th percentile have only 60 power plants. The small number of countries with a huge number of power plants don't make the mean incorrect, but certainly make it less useful as a way to understand the data.
Create a pie plot showing the distribution of different fuel types for the number of power plants around the world. Now show a similar pie plot showing the total MW capacity of different fuel types.
I was interested in this data set because of the news about nuclear power. But just how many nuclear plants are there in the world?
Once again, we can use value_counts, this time on the primary_fuel column, to find out how many plants there are of each type:
(
gdf
['primary_fuel']
.value_counts()
)We can create a pie plot from within Pandas using the plot.pie method. But because value_counts sorts its results from most to least common, the pie chart ended up with the smallest values together – rendering their labels pretty much unreadable. I thus decided to run sort_index on the result from value_counts, to get them into an order that was a bit easier to read.
(I also tried passing sort=False to value_counts, but that resulted in an equally hard-to-read set of labels. I also tried to randomize the output with sample(frac=1), but never managed to get a consistently spread-out set of data. So I'm sticking with this, which mostly works.)
Here, then, is my query:
(
gdf
['primary_fuel']
.value_counts()
.sort_index()
.plot.pie()
)And here's the plot I got:

This pie plot really confused me: Are solar, hydro, and wind plants really that popular? So much more than coal, gas, and oil?
The answer, of course, is "no": We're measuring the number of plants here, not their capacity. A very large number of tiny solar plants will add to the count, but not necessarily to the generative capacity.
That's why I then asked you to create a pie plot comparing the capacity of each type of fuel.
To do that, we'll use groupby on the primary_fuel column, getting one result row for each. We'll calculate the sum of the rows for each type of fuel, adding together the values in the capacity_mw column:
(
gdf
.groupby('primary_fuel')['capacity_mw'].sum()
)We can see the results here:
primary_fuel
Biomass 3.428130e+04
Coal 1.965541e+06
Cogeneration 4.048000e+03
Gas 1.493051e+06
Geothermal 1.268775e+04
Hydro 1.053160e+06
Nuclear 4.079118e+05
Oil 2.618787e+05
Other 3.612860e+03
Petcoke 2.424577e+03
Solar 1.883123e+05
Storage 1.712300e+03
Waste 1.474871e+04
Wave and Tidal 5.522000e+02
Wind 2.630537e+05
Name: capacity_mw, dtype: float64I don't know about you, but I find this a bit hard to read. And yes, it'll be solved when we create a pie plot, but it's still a bit hard to read.
There's an easy solution: The Pandas developers provide a settable option, set_eng_float_format, which lets us indicate how many decimal digits we want to see, and whether it should use prefixes for the quantities:
pd.set_eng_float_format(accuracy=3, use_eng_prefix=True)
The result:
primary_fuel
Biomass 34.281k
Coal 1.966M
Cogeneration 4.048k
Gas 1.493M
Geothermal 12.688k
Hydro 1.053M
Nuclear 407.912k
Oil 261.879k
Other 3.613k
Petcoke 2.425k
Solar 188.312k
Storage 1.712k
Waste 14.749k
Wave and Tidal 552.200
Wind 263.054k
Name: capacity_mw, dtype: float64And of course, if we want, we can sort the results:
primary_fuel
Wave and Tidal 552.200
Storage 1.712k
Petcoke 2.425k
Other 3.613k
Cogeneration 4.048k
Geothermal 12.688k
Waste 14.749k
Biomass 34.281k
Solar 188.312k
Oil 261.879k
Wind 263.054k
Nuclear 407.912k
Hydro 1.053M
Gas 1.493M
Coal 1.966M
Name: capacity_mw, dtype: float64In other words, coal, gas, and hydro are generating far more energy than the other technologies. (I didn't know that hydro was generating that much, to be honest.) Nuclear is in fourth place, when calculated worldwide.
To get our pie plot, we can say:
(
gdf
.groupby('primary_fuel')['capacity_mw'].sum()
.plot.pie()
)
(I removed the call to sort_values to avoid clashes between small ones.)
The resulting plot:

Nuclear is not an insignificant source of energy around the world, but it's still small compared with coal, gas, and hydro.
What country has the greatest proportion of its power-generating capacity coming from nuclear plants?
Let's start by grabbing just the three columns we'll need to answer this question, country_long, capacity_mw, and primary_fuel.
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
)We'll want to be able to distinguish easily between those rows with nuclear fuel and those with others. So I used assign to create a new column, nuclear_fuel, which will have a True or False value:
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
)Now we want to get the total capacity for each country, both using nuclear fuel and non-nuclear fuel. We can thus use groupby on two categorical columns by passing a list. We'll be calculating on capacity_mw, and will use the sum method:
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
.groupby(['country_long', 'nuclear_fuel'])['capacity_mw'].sum()
)The result is a series with a two-level multi-index. The values in the series represent the amount of capacity. Rows in which nuclear_fuel is True describe total nuclear capacity, while those with False describe those with non-nuclear capacity. Note that countries that have no nuclear power will only have a single row in the series. (In theory, a country that has only nuclear power will also have a single row, but no such country exists, from what I can tell.)
Next, we can turn this multi-indexed series into a pivot table, using the unstack method. That will leave a bunch of NaN values in the resulting data frame, typically where there was no nuclear capacity for that country. I used fillna to replace those NaN values with 0:
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
.groupby(['country_long', 'nuclear_fuel'])['capacity_mw'].sum()
.unstack('nuclear_fuel')
.fillna(0)
)We now have a data frame whose rows are country names, and with two columns, True (nuclear capacity) and False (non-nuclear capacity). But we want to know the proportion of power used by nuclear capacity. We can do this by dividing the value in the True column by the total for the row. I decided to use assign here:
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
.groupby(['country_long', 'nuclear_fuel'])['capacity_mw'].sum()
.unstack('nuclear_fuel')
.fillna(0)
.assign(nuclear_proportion= lambda df_: df_[True] / df_.sum(axis='columns'))
)The resulting nuclear_proportion column is a float. For the float values to be displayed reasonably, I turned off the formatting we did above:
pd.set_option("display.float_format", None)
We can then retrieve that column, sort the values in descending order, and grab the top 10:
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
.groupby(['country_long', 'nuclear_fuel'])['capacity_mw'].sum()
.unstack('nuclear_fuel')
.fillna(0)
.assign(nuclear_proportion= lambda df_: df_[True] / df_.sum(axis='columns'))
['nuclear_proportion']
.sort_values(ascending=False)
.head(10)
)The 10 countries with the greatest proportion of nuclear capacity are:
country_long
France 0.570713
Belgium 0.442066
Sweden 0.369511
Hungary 0.299935
Ukraine 0.284848
Slovakia 0.282465
Switzerland 0.261473
Slovenia 0.254386
Czech Republic 0.247337
Finland 0.233284
Name: nuclear_proportion, dtype: float64I had long heard that France used a great deal of nuclear power, but I hadn't ever thought about most of the other countries on this list. Ukraine's nuclear reactors have been in the news since the Russian invasion more than two years ago, but I didn't realize that nearly 30 percent of its power came from nuclear.
Where is the United States on this list? Let's find out:
pd.set_option("display.float_format", None)
(
gdf
[['country_long', 'capacity_mw', 'primary_fuel']]
.assign(nuclear_fuel = lambda df_: df_['primary_fuel'] == 'Nuclear')
.groupby(['country_long', 'nuclear_fuel'])['capacity_mw'].sum()
.unstack('nuclear_fuel')
.fillna(0)
.assign(nuclear_proportion= lambda df_: df_[True] / df_.sum(axis='columns'))
['nuclear_proportion']
.sort_values(ascending=False)
.loc['United States of America']
)The result? Only 8.6 percent.
Create an interactive map for all power plants whose capacity is in the top 50%. The color of each dot should be based on the primary_fuel column. Hovering over a dot should show the country name, the power plant's name, the fuel type, and the capacity in MW.
First, let's keep only those rows in which the capacity_mw column is in the top 50%. (Meaning the median, rather than the mean.) I did that using loc along with lambda:
(
gdf
.loc[lambda df_: df_['capacity_mw'] >= df_['capacity_mw'].median()]
)With that in place, I chose just a few columns. Not only did I find that it speeds up the processing of the map, but it also reduces the number of columns shown in the tooltip. (You can also pass keyword arguments to adjust which tooltips are shown.) We then invoke explore, which (inside of Jupyter) displays an interactive map in which our points are all displayed.
The coloring of the dots depends on the argument passed to explore. In this case, I passed primary_fuel, which means that the dots will be colored according to the type of fuel they represent. By adding legend=True, we can know which color represents which fuel type:
(
gdf
.loc[lambda df_: df_['capacity_mw'] >= df_['capacity_mw'].median()]
[['country_long', 'name', 'capacity_mw', 'geometry', 'primary_fuel']]
.explore('primary_fuel', legend=True)
)Here's a screenshot of the map that I got:

Create an interactive map for all of the nuclear power plants. The color of each dot should be based on the capacity_mw column. Hovering over a dot should show the country name, the power plant's name, and the capacity in MW.
Finally, I asked you to create another interactive map, this time only for nuclear plants. The colorizing this time should represent the generative capacity of each plant.
The query is quite similar to what we did in the previous question, but now we're filtering based on the primary_fuel column. The resulting query is:
(
gdf
.loc[lambda df_: df_['primary_fuel'] == 'Nuclear']
[['country_long', 'name', 'capacity_mw', 'geometry']]
.explore('capacity_mw', legend=True)
)Here's the resulting map:

I hope that you enjoyed this week's data set and exploration! As always, comments, suggestions, and thoughts are welcome
The Jupyter notebook that I used is here: https://drive.google.com/file/d/1sL2XWQc1H881-oFdAJIFK3-jFfXU1xHy/view?usp=sharing
Reuven