Exploring Palmer Penguins with Python and R

Author

Louise Izere

Published

September 25, 2026

The data comes from the Palmer Penguins project.

License: CC0 (Creative Commons Zero)

Citation: Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) penguin data. R package version 0.1.0. doi: 10.5281/zenodo.3960218.

In this post, I use both R and Python with the help of Reticulate, to analyze the Palmer Penguins dataset. I first calculate the average penguin body mass in R, then pass that result to Python and use it in a second analysis.

library(palmerpenguins)
library(reticulate)

penguins <- na.omit(penguins)

average_mass <- mean(penguins$body_mass_g)

average_mass
[1] 4207.057

The R code calculates the average body mass of the penguins and stores it in average_mass.

Next, I access the R object from Python using r.average_mass.

average_mass_python = r.average_mass

average_mass_python
4207.057057057057

The value calculated in R is now available in Python. I can use it to determine whether each penguin has a body mass above or below the overall average.

from palmerpenguins import load_penguins
import altair as alt

penguins_py = load_penguins().dropna()

penguins_py["mass_group"] = penguins_py["body_mass_g"].apply(
    lambda x: "Above average" if x > average_mass_python else "At or below average"
)

penguins_py[["species", "body_mass_g", "mass_group"]].head()
  species  body_mass_g           mass_group
0  Adelie       3750.0  At or below average
1  Adelie       3800.0  At or below average
2  Adelie       3250.0  At or below average
4  Adelie       3450.0  At or below average
5  Adelie       3650.0  At or below average

Finally, I visualize the result in Python. The graph below shows how many penguins are below or above the average mass in each species.

alt.Chart(penguins_py).mark_bar().encode(
    x=alt.X("species:N", title="Species"),
    y=alt.Y("count():Q", title="Number of penguins"),
    color=alt.Color("mass_group:N", title="Body mass"),
    column="mass_group:N"
)
Figure 1: Penguin body mass compared to the average.