<jupyter_start><jupyter_text>Analysis of Planet-Moon Data<jupyter_code>#importing the libraries
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Part (1) Importing the data<jupyter_code>#Reading the datasets, the table is the 6th table in the list hence we select the 6th using [5]
moonDF = (pd.read_html('https://en.wikipedia.org/wiki/List_of_natural_satellites#List.html', 
                   header = 0)
      [5])
#Lets look at the dataframe
moonDF.head()<jupyter_output><empty_output><jupyter_text>Part 2 (a) Renaming the columns for easy understanding<jupyter_code>#Renaming the columns
(moonDF.rename(columns = {'Image': 'image', 
                          'Parent': 'planet', 
                          'Numeral': 'number', 
                          'Mean radius (km)': 'avg_radius', 
                          'Semi-major axis (km)': 'sm_axis', 
                          'Sidereal period (d) (r = retrograde)': 'revolution_time', 
                          'Discovery year': 'discovery_year', 
                          'Discovered by': 'discovered_by', 
                          'Notes': 'notes', 
                          'Ref(s)': 'references', 
                          'Name': 'moon'}, 
               inplace = True))
#Let's look at the DF
moonDF.head()<jupyter_output><empty_output><jupyter_text>Part 2 (b) Reordering the columns for better understanding<jupyter_code>#Reordering the column for better understanding
moonDF = (moonDF[['planet', 'moon', 'number', 'discovery_year', 'discovered_by',
                  'avg_radius', 'sm_axis', 'revolution_time', 'notes', 
                  'references', 'image']])
#Let's look at the data again
moonDF.head()<jupyter_output><empty_output><jupyter_text>Part 2 (c) Setting the column 'planet' and 'moon' to form a two level index<jupyter_code>#Setting the index as planet and then the moon
(moonDF.set_index(['planet', 'moon'], inplace = True))
#Let's look at the dataframe
moonDF.head()
#Removing the image column because it has none values
moonDF.drop(columns = ['image'], inplace = True)<jupyter_output><empty_output><jupyter_text>Part 2 (d) Functions to clean the columns<jupyter_code>#For this function we first need to remove the ',' from the string and then extraxt the first floating number in the string
def cleanNumericData(s):
  num = str(s).replace(',', '')
  return float(re.search(r'\d*\.\d+|\d+', num).group())


#Function to clean the references column to make it a list of numeric referenes
def cleanReferences(s):
  splitted = str(s).split('][')
  splittedClear = [str(item).replace('[','').replace(']','') for item in splitted]
  return splittedClear


#Function to clean the numeral (Now called number column to just show the numeric count the moon, remove the roman value)
def cleanNumberColumn(s):
  return s[s.find("(")+1:s.find(")")]
#Applying the function to avg_radius, revolution_time and sm_axis column to get just the main floating point number
moonDF.avg_radius = moonDF.avg_radius.apply(cleanNumericData)
moonDF['sm_axis'] = moonDF.sm_axis.apply(cleanNumericData)
moonDF['revolution_time'] = moonDF.revolution_time.apply(cleanNumericData)

#Applying the function to the references column
moonDF['references'] = moonDF.references.apply(cleanReferences)

#Applying the function to the cleanNumberColumn to the number column
moonDF['number'] = moonDF.number.apply(cleanNumberColumn)
#Let's look at the dataframe
moonDF.head()<jupyter_output><empty_output><jupyter_text>Part (3)  I'm trying to answer the question that what is the average radius of the revolution orbit of the moons, grouped by planets?<jupyter_code>#What is the average radius of the moon per planet for the top 5 avg radii.
avgMoonDF = moonDF.reset_index().groupby(by = ['planet']).mean().sort_values(by = ['sm_axis'], ascending = False).head(5)
avgMoonDF
plt.bar(avgMoonDF.index, avgMoonDF.sm_axis)
plt.title('Average Radii of the moons for various planets')
plt.xlabel('Planet')
plt.ylabel('Avg Radii in KM (10^7)')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11802154750244857, 'fraction_numerical': 0.00416258570029383, 'mean_word_length': 3.9575242718446604, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '173062', 'license': 'MIT', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1274, 'n_words': 378, 'path': 'planet_moon_analysis.ipynb', 'repo_name': 'abhishekmanglaa/planet-moon-analysis-python', 'size': '4084'}