Data Cleaning with Pandas Library
Retrieve Data Frame Top 15 Enery Ranking
Retrieve Data Frame with the top ten Country information the Energy Ranking.
For more details click on the button "show code" in the next section, to show step-by-step the preprocessing of the data.
| Rank | Documents | Citable documents | Citations | Self-citations | Citations per document | H index | Energy Supply | Energy Supply per Capita | % Renewable | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Country | ||||||||||||||||||||
| China | 1 | 127050 | 126767 | 597237 | 411683 | 4.70 | 138 | 127191000000 | 93 | 19.754910 | 3.992331e+12 | 4.559041e+12 | 4.997775e+12 | 5.459247e+12 | 6.039659e+12 | 6.612490e+12 | 7.124978e+12 | 7.672448e+12 | 8.230121e+12 | 8.797999e+12 |
| United States | 2 | 96661 | 94747 | 792274 | 265436 | 8.20 | 230 | 90838000000 | 286 | 11.570980 | 1.479230e+13 | 1.505540e+13 | 1.501149e+13 | 1.459484e+13 | 1.496437e+13 | 1.520402e+13 | 1.554216e+13 | 1.577367e+13 | 1.615662e+13 | 1.654857e+13 |
| Japan | 3 | 30504 | 30287 | 223024 | 61554 | 7.31 | 134 | 18984000000 | 149 | 10.232820 | 5.496542e+12 | 5.617036e+12 | 5.558527e+12 | 5.251308e+12 | 5.498718e+12 | 5.473738e+12 | 5.569102e+12 | 5.644659e+12 | 5.642884e+12 | 5.669563e+12 |
| United Kingdom | 4 | 20944 | 20357 | 206091 | 37874 | 9.84 | 139 | 7920000000 | 124 | 10.600470 | 2.419631e+12 | 2.482203e+12 | 2.470614e+12 | 2.367048e+12 | 2.403504e+12 | 2.450911e+12 | 2.479809e+12 | 2.533370e+12 | 2.605643e+12 | 2.666333e+12 |
| Russian Federation | 5 | 18534 | 18301 | 34266 | 12422 | 1.85 | 57 | 30709000000 | 214 | 17.288680 | 1.385793e+12 | 1.504071e+12 | 1.583004e+12 | 1.459199e+12 | 1.524917e+12 | 1.589943e+12 | 1.645876e+12 | 1.666934e+12 | 1.678709e+12 | 1.616149e+12 |
| Canada | 6 | 17899 | 17620 | 215003 | 40930 | 12.01 | 149 | 10431000000 | 296 | 61.945430 | 1.564469e+12 | 1.596740e+12 | 1.612713e+12 | 1.565145e+12 | 1.613406e+12 | 1.664087e+12 | 1.693133e+12 | 1.730688e+12 | 1.773486e+12 | 1.792609e+12 |
| Germany | 7 | 17027 | 16831 | 140566 | 27426 | 8.26 | 126 | 13261000000 | 165 | 17.901530 | 3.332891e+12 | 3.441561e+12 | 3.478809e+12 | 3.283340e+12 | 3.417298e+12 | 3.542371e+12 | 3.556724e+12 | 3.567317e+12 | 3.624386e+12 | 3.685556e+12 |
| India | 8 | 15005 | 14841 | 128763 | 37209 | 8.58 | 115 | 33195000000 | 26 | 14.969080 | 1.265894e+12 | 1.374865e+12 | 1.428361e+12 | 1.549483e+12 | 1.708459e+12 | 1.821872e+12 | 1.924235e+12 | 2.051982e+12 | 2.200617e+12 | 2.367206e+12 |
| France | 9 | 13153 | 12973 | 130632 | 28601 | 9.93 | 114 | 10597000000 | 166 | 17.020280 | 2.607840e+12 | 2.669424e+12 | 2.674637e+12 | 2.595967e+12 | 2.646995e+12 | 2.702032e+12 | 2.706968e+12 | 2.722567e+12 | 2.729632e+12 | 2.761185e+12 |
| South Korea | 10 | 11983 | 11923 | 114675 | 22595 | 9.57 | 104 | 11007000000 | 221 | 2.279353 | 9.410199e+11 | 9.924316e+11 | 1.020510e+12 | 1.027730e+12 | 1.094499e+12 | 1.134796e+12 | 1.160809e+12 | 1.194429e+12 | 1.234340e+12 | 1.266580e+12 |
Retrieve the Average Energy by Country
Retrieve Data Frame with the top ten Country information the Energy Ranking.
top15_avg_condition = data[['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']]
top15_avg_rank = top15_avg_condition.mean(axis=1).sort_values(ascending=False)
| Country | Average |
|---|---|
| United States | 15364344302990,0 |
| China | 6348608932836,1 |
| Japan | 5542207638235,176 |
| Germany | 3493025339072,848 |
| France | 2681724635761,589 |
| United Kingdom | 2487906661418,4175 |
| India | 1769297396603,8599 |
| Canada | 1660647466307,512 |
| Russian Federation | 1565459478480,661 |
| South Korea | 1106714508244,852 |
Calculate GDP Fluctuation over a 10-year
In this analysis, we aim to calculate the extent of GDP fluctuation over a 10-year duration for the nation holding the 4th position in terms of its average GDP. The provided code calculates the absolute GDP growth for the country with the third-highest average GDP across the years 2006 to 2015 and identifies the name of that country.
data['avg_gdp'] = data[['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015']].mean(axis=1)
data.sort_values("avg_gdp", ascending=False, inplace=True)
Germany is the Country with the 4th place in the absolute GDP growth, with a value of 352665152317,9.
Lowest Percentage of Renewable Energy
Retrieve the country with the lowest percentage of renewable energy and reporting the name of the country along with its corresponding percentage.
lower_index = data['% Renewable'].idxmin()
lower_percentage = round(data['% Renewable'].min(), 1)
South Korea is the Country with the lowest percentage of renewable energy, and its value is 2.3%.
Ratio of Self-Citations to Total Citations
Calculate a new column representing the ratio of Self-Citations to Total Citations. Determine the maximum value within this new column and identify the country with the highest ratio.
data['Ratio'] = data['Self-citations'] / data['Citations']
lower_index_ratio = data['Ratio'].idxmin()
lower_percentage_ratio = round(data['Ratio'].min(), 3)
United Kingdom is the Country with the lowest ration of Self-Citations, and its value is 0,184.
Estimate the population by using the Energy Supply and Energy Supply per Capita values
Generate a new column to estimate the population by using the Energy Supply and Energy Supply per capita values. Determine the third most populous country based on this population estimate. Convert columns to numeric and handle non-numeric values
data['Energy Supply'] = pd.to_numeric(data['Energy Supply'], errors='coerce')
data['Energy Supply per Capita'] = pd.to_numeric(data['Energy Supply per Capita'], errors='coerce')
data['Energy Supply'].fillna(0, inplace=True)
data['Energy Supply per Capita'].fillna(0, inplace=True)
data['PopEstimate'] = (data['Energy Supply'] / data['Energy Supply per Capita']).round()
data.sort_values('PopEstimate', ascending=False, inplace=True)
United States is the Country with the third ation of Self-Citations, and its value is 317615385,0.
Ratio of citable documents / Energy Supply per Person
Generation a new column that estimates the ratio of citable documents to the population (per person). Calculate the correlation between this ratio and the energy supply per capita using Pearson's correlation coefficient via the .corr() method.
data['PopEstimate'] = data['Energy Supply'] / data['Energy Supply per Capita']
data['PopCitableDocuments'] = data['Citable documents'] / data['PopEstimate']
correlation = data['PopCitableDocuments'].astype(float).corr(data['Energy Supply per Capita'].astype(float))
A correlation coefficient of 0,750683471027858 suggests a strong positive linear relationship between the ratio of citable documents to the population (per person) and the energy supply per capita. In other words, as the energy supply per capita increases, there tends to be a corresponding increase in the ratio of citable documents to the population. This indicates that countries with higher energy supply per capita tend to have more citable documents per person, implying a potential connection between energy availability and scientific research output.
Ratio of citable documents / Energy Supply per Person
Generation a new column that estimates the ratio of citable documents to the population (per person). Calculate the correlation between this ratio and the energy supply per capita using Pearson's correlation coefficient via the .corr() method. Use the matplotlib built-in function plot() to visualize the relationship between Energy Supply per Capita and Citable Documents per Capita. Install the matplotlib, BytesIO, & base64 library in your virtual environment and include the necessary import statement in the file's header. Save the plot to a BytesIO object, and next Encode the image to base64.
data['PopEstimate'] = data['Energy Supply'] / data['Energy Supply per Capita']
data['PopCitableDocuments'] = data['Citable documents'] / data['PopEstimate']
correlation = data['PopCitableDocuments'].astype(float).corr(data['Energy Supply per Capita'].astype(float))
A correlation coefficient of 0,750683471027858 suggests a strong positive linear relationship between the ratio of citable documents to the population (per person) and the energy supply per capita. In other words, as the energy supply per capita increases, there tends to be a corresponding increase in the ratio of citable documents to the population. This indicates that countries with higher energy supply per capita tend to have more citable documents per person, implying a potential connection between energy availability and scientific research output.
Matches or Exceeds the Median value
Using DataFrame tools, set up a column containing a '1' if a country's % Renewable value matches or exceeds the median value among all countries within the top 15 rankings. Conversely, assign a '0' if the country's % Renewable value falls below the median. The resulting output will be a series named "High Renewable," and its index will be sorted in ascending order based on the country's rank.
mediana_value = data['% Renewable'].median()
data['HighRenewable'] = data.apply(lambda x: 1 if x['% Renewable'] >= mediana_value else 0, axis=1)
data.sort_values('Rank',ascending=True,inplace=True)
The value median calculated is 15,99, for Top Ten Countries in the Ranking
| Rank | % Renewable | HighRenewable | |
|---|---|---|---|
| Country | |||
| China | 1 | 19.754910 | 1 |
| United States | 2 | 11.570980 | 0 |
| Japan | 3 | 10.232820 | 0 |
| United Kingdom | 4 | 10.600470 | 0 |
| Russian Federation | 5 | 17.288680 | 1 |
| Canada | 6 | 61.945430 | 1 |
| Germany | 7 | 17.901530 | 1 |
| India | 8 | 14.969080 | 0 |
| France | 9 | 17.020280 | 1 |
| South Korea | 10 | 2.279353 | 0 |
Create Groups and Key Statistics
Utilize the provided dictionary, defined as "ContinentDict," to group countries by their respective continents. Subsequently, construct a DataFrame that presents key statistics for each continent, including the sample size (number of countries), the sum, mean, and standard deviation of the estimated population of the countries within that continent.
Divide the '% Renewable' values into five distinct bins. Afterward, categorize the 'Top15' dataset by both continent and the newly defined bins for '% Renewable.' Determine the count of countries in each of these combined groupings.
The desired outcome is a Series that incorporates a MultiIndex structure, first based on the continent, and then further sub-divided by the bins representing '% Renewable.' Exclude groups that have no countries within them.
data_by_continent = pd.DataFrame(columns=['size', 'sum', 'mean', 'std'])
for idx, name in data.groupby(ContinentDict):
data_by_continent.loc[idx] = [len(name), name['PopEstimate'].sum(), name['PopEstimate'].mean(), name['PopEstimate'].std()]
data['ByContinent'] = data.index.to_series().map(ContinentDict)
data['SubGroups'] = pd.cut(data['% Renewable'], 5)
data_by_continent_bins = data.groupby(['ByContinent', 'SubGroups']).size()
data_by_continent_bins = data_by_continent_bins[data_by_continent_bins > 0]
| Continent | Bins | size |
|---|---|---|
| Asia | (2.22, 14.213] | |
| 2 | ||
| Asia | (14.213, 26.146] | |
| 2 | ||
| Europe | (2.22, 14.213] | |
| 1 | ||
| Europe | (14.213, 26.146] | |
| 3 | ||
| North America | (2.22, 14.213] | |
| 1 | ||
| North America | (50.012, 61.945] | |
| 1 |