A Planets Dataset in Python & R


Here is a very small sample dataset for practicing your coding skills in Python and pandas. It’s just for demonstration purposes. I like small datasets when I’m learning new things because it makes the before and after easy to see. The library we use here is pandas.

Below is a small dataset of the planets. You can run this in Jupyter Notebook.

import pandas as pd
# Define df1
data = {'planet': ['Mercury', 'Venus', 'Earth', 'Mars',
                    'Jupiter', 'Saturn', 'Uranus', 'Neptune'],
        'radius_km': [2440, 6052, 6371, 3390, 69911, 58232,
                      25362, 24622],
        'moons': [0, 0, 1, 2, 80, 83, 27, 14],
        'distance_AU': [0.39,0.72,1.0,1.52,5.2,9.54,19.8,30.06]
         }
df1 = pd.DataFrame(data)
df1

Here below is a dataset that is missing value 19.22 from Uranus.

# Define df2
data = {'planet': ['Mercury', 'Venus', 'Earth', 'Mars',
                    'Jupiter', 'Saturn', 'Uranus', 'Neptune'],
        'radius_km': [2440, 6052, 6371, 3390, 69911, 58232,
                      25362, 24622],
        'moons': [0, 0, 1, 2, 80, 83, 27, 14],
        'distance_AU': [0.39,0.72,1.0,1.52,5.2,9.54,None,30.06]
         }
df2 = pd.DataFrame(data)
df2

We can give a name to the df1’s first column, which is the index. df1.index.name = ‘index’. You can do the same for df2, if you want. I did it because I want to export the DataFrame to a csv file and I want to have that first column named. To export I would use the to_csv() function like this: df1.to_csv(r’C:\MyDataFiles\Planets\planets1.csv’). Change the folder and file name to your preference.

R Language

Here below is the code to create the same dataset using RStudio. Here we are creating a data frame manually.

planet <- c('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')
radius_km <- c(2440, 6052, 6371, 3390, 69911, 58232, 25362, 24622)
moons <- c(0, 0, 1, 2, 80, 83, 27, 14)
distance_AU <- c(0.39,0.72,1.0,1.52,5.2,9.54,19.8,30.06)
planets <- data.frame(planet, radius_km, moons, distance_AU)

Pluto is not considered to be a planet. It’s still out there, with its atmosphere, moons and it orbit around the sun. According to the IAU, Pluto is technically a “dwarf planet,” because it has not “cleared its neighboring region of other objects.” This means that Pluto still has lots of asteroids and other space rocks along its flight path, rather than having absorbed them over time, like the larger planets have done.

Planets CSV File

Here is the planets1.csv file in a comma-separated-values text file that you can download.

Leave a Reply