TANZANIA GROSS DOMESTIC PRODUCT (GDP) ANALYSIS FROM 2012-2022 AT CONSTANT 2015 PRICES¶

## Raw data(set) obtained from https://www.nbs.go.tz/nbs/takwimu/na/First_Quarter_GDP_2023.xls ##

Data cleaned, prepared, and analyzed by Moiz A. Rashid - Data Analyst & Database Administrator on 06/10/2023¶

In [1]:
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
df = pd.read_excel("TZ_GDP_2012_2022_Constant_2015_prices.xlsx")
In [3]:
df.head()
Out[3]:
Year Quarter kilimo Madini na Mawe Viwanda Umeme Maji safi na maji taka Ujenzi Biashara na Matengenezo Malazi na Huduma ya Chakula ... Fedha na Bima Utawala na Ulinzi Sughuli za kitaaluma, Sayansi na Ufundi Huduma zinazohusiana na Utawala Upangishaji Majengo Elimu Afya na Huduma za Jami Huduma nyingine za Kijamii Ongeza kodi katika bidhaa Pato la Taifa (GDP) kwa bei za soko
0 2012 Q1 5.972854e+06 845470.039314 1.528195e+06 151473.241045 86422.042558 1.816993e+06 1.877418e+06 321003.350791 ... 849590.228292 8.036420e+05 74831.254064 340487.060189 641698.126652 461651.689201 309227.715282 217530.371977 1.732167e+06 1.952108e+07
1 2012 Q2 5.182996e+06 825382.965908 1.544666e+06 164426.553260 89318.723497 1.808760e+06 1.855404e+06 320427.451827 ... 844631.387966 8.170567e+05 78602.910444 347904.539288 648275.934724 447190.745884 320506.093904 221442.796821 1.596603e+06 1.887328e+07
2 2012 Q3 4.092001e+06 833592.190166 1.499895e+06 172945.771478 92525.732354 2.012312e+06 1.725714e+06 358248.167846 ... 886247.292356 9.760766e+05 82450.849334 358075.610766 654898.666322 505099.006612 339618.224762 234892.011125 1.737034e+06 1.837880e+07
3 2012 Q4 6.559178e+06 810297.012355 1.494233e+06 180223.063372 90094.224021 1.940197e+06 1.912875e+06 344244.768768 ... 863692.107093 1.026348e+06 86375.070732 371000.274624 661566.321444 508701.534676 315241.677663 236523.000986 1.895967e+06 2.120669e+07
4 2013 Q1 5.994220e+06 748411.734006 1.459422e+06 167460.032112 87305.707030 2.146785e+06 1.828162e+06 332454.437309 ... 811387.105144 8.974361e+05 90375.574640 386678.530860 668324.541790 454978.600355 299454.170867 239370.081799 1.947582e+06 2.039155e+07

5 rows × 22 columns

In [4]:
df['Date'] = df['Year'].astype(str) + df['Quarter'].astype(str)
df['Date'] = pd.to_datetime(df['Date'])
C:\Users\Administrator\AppData\Local\Temp\1\ipykernel_11572\1675793848.py:2: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.
  df['Date'] = pd.to_datetime(df['Date'])
In [5]:
df.set_index('Date', inplace=True)
In [6]:
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Pato la Taifa (GDP) kwa bei za soko'], marker='o', linestyle='-')
plt.title('Tanzania GDP Over Time')
plt.xlabel('Year')
plt.ylabel('GDP (Constant 2015 Prices)')
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
In [7]:
# Create a box plot for quarterly GDP distribution
plt.figure(figsize=(10, 6))
df.boxplot(column='Pato la Taifa (GDP) kwa bei za soko', by='Quarter', grid=False)
plt.title('Quarterly GDP Distribution')
plt.suptitle('')
plt.xlabel('Quarter')
plt.ylabel('GDP (Constant 2015 Prices)')
plt.xticks(rotation=45)
plt.show()
<Figure size 1000x600 with 0 Axes>
In [8]:
# Create a time series line chart for the "Umeme" sector
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Umeme'], marker='o', linestyle='-', color='b')
plt.title('Electricity Sector GDP Over Time')
plt.xlabel('Year')
plt.ylabel('GDP (Constant 2015 Prices)')
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
In [9]:
# List of all sectors
all_sectors = ['kilimo', 'Madini na Mawe', 'Viwanda', 'Umeme', 'Maji safi na maji taka', 'Ujenzi', 'Biashara na Matengenezo',
               'Malazi na Huduma ya Chakula', 'Uchukuzi na Uhifadhi Mizigo', 'Habari na Mawasiliano', 'Fedha na Bima',
               'Utawala na Ulinzi', 'Sughuli za kitaaluma, Sayansi na Ufundi', 'Huduma zinazohusiana na Utawala',
               'Upangishaji Majengo', 'Elimu', 'Afya na Huduma za Jami', 'Huduma nyingine za Kijamii', 
               'Ongeza kodi katika bidhaa']

# Create subplots for each sector
fig, axes = plt.subplots(nrows=5, ncols=4, figsize=(16, 16), sharex=True)
fig.subplots_adjust(hspace=0.5)

for i, sector in enumerate(all_sectors):
    row, col = i // 4, i % 4
    ax = axes[row, col]

    ax.plot(df.index, df[sector], marker='o', linestyle='-', label=sector)
    ax.set_title(sector)
    ax.set_xlabel('Year')
    ax.set_ylabel('GDP (Constant 2015 Prices)')
    ax.grid(True)
    ax.legend(loc='upper left', fontsize='small')

# Remove empty subplots
for i in range(len(all_sectors), len(axes.flat)):
    fig.delaxes(axes.flatten()[i])

# Rotate x-axis labels for all subplots
for ax in axes.flat:
    ax.tick_params(axis='x', rotation=45)

plt.tight_layout()
plt.show()
In [10]:
quarterly_data = df.groupby(['Year', 'Quarter'])['Pato la Taifa (GDP) kwa bei za soko'].sum().unstack()
quarterly_data.plot(kind='line', marker='o', figsize=(12, 6))
plt.title('Seasonal Patterns in Tanzania GDP')
plt.xlabel('Year')
plt.ylabel('GDP (Constant 2015 Prices)')
plt.grid(True)
plt.xticks(rotation=45)
plt.legend(title='Quarter', loc='upper left', labels=['Q1', 'Q2', 'Q3', 'Q4'])
plt.show()
In [11]:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Load the dataset (if not already loaded)
# df = pd.read_excel("TZ_GDP_2012_2022_Constant_2015_prices.xlsx")
# df.set_index('Date', inplace=True)

# List of sectors
sectors = ['kilimo', 'Madini na Mawe', 'Viwanda', 'Umeme', 'Maji safi na maji taka', 'Ujenzi',
           'Biashara na Matengenezo', 'Malazi na Huduma ya Chakula', 'Uchukuzi na Uhifadhi Mizigo',
           'Habari na Mawasiliano', 'Fedha na Bima', 'Utawala na Ulinzi',
           'Sughuli za kitaaluma, Sayansi na Ufundi', 'Huduma zinazohusiana na Utawala', 'Upangishaji Majengo',
           'Elimu', 'Afya na Huduma za Jami', 'Huduma nyingine za Kijamii', 'Ongeza kodi katika bidhaa']

# Create a new DataFrame for sectoral growth rates
growth_df = df[sectors].pct_change() * 100  # Calculate percentage change as growth rate
growth_df['Year'] = df['Year']  # Add the 'Year' column to the new DataFrame

# Set the style of the plot
sns.set(style="whitegrid")
plt.figure(figsize=(12, 8))

# Plot growth rates for each sector
for sector in sectors:
    sns.lineplot(x='Year', y=sector, data=growth_df, label=sector)

plt.title('Sectoral Growth Rates Over Time')
plt.xlabel('Year')
plt.ylabel('Growth Rate (%)')
plt.grid(True)
plt.xticks(rotation=45)
plt.legend(loc='upper left', bbox_to_anchor=(1, 1), fontsize='small')
plt.tight_layout()
plt.show()
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
In [12]:
# Extract data for the "Elimu" sector
education_data = df[['Year', 'Quarter', 'Elimu']]
In [13]:
import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 6))
sns.lineplot(data=education_data, x='Year', y='Elimu', marker='o', linestyle='-')
plt.title('Contribution of Education Sector to GDP Over Time')
plt.xlabel('Year')
plt.ylabel('GDP (Constant 2015 Prices)')
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
  with pd.option_context('mode.use_inf_as_na', True):
In [14]:
education_and_technical_data = df[['Year', 'Quarter', 'Elimu', 'Sughuli za kitaaluma, Sayansi na Ufundi']]
In [15]:
import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
sns.scatterplot(data=education_and_technical_data, x='Elimu', y='Sughuli za kitaaluma, Sayansi na Ufundi', hue='Year', palette='viridis')
plt.title('Relationship Between Education and Professional/Technical Activities')
plt.xlabel('Education Sector GDP (Constant 2015 Prices)')
plt.ylabel('Professional/Technical Activities Sector GDP (Constant 2015 Prices)')
plt.grid(True)
plt.legend(title='Year', bbox_to_anchor=(1, 1))
plt.show()
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
C:\Users\Administrator\anaconda3\lib\site-packages\seaborn\_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
In [16]:
pip install bokeh
Requirement already satisfied: bokeh in c:\users\administrator\anaconda3\lib\site-packages (2.4.3)
Requirement already satisfied: typing-extensions>=3.10.0 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (4.4.0)
Requirement already satisfied: PyYAML>=3.10 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (6.0)
Requirement already satisfied: numpy>=1.11.3 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (1.23.5)
Requirement already satisfied: Jinja2>=2.9 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (3.1.2)
Requirement already satisfied: packaging>=16.8 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (22.0)
Requirement already satisfied: pillow>=7.1.0 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (9.4.0)
Requirement already satisfied: tornado>=5.1 in c:\users\administrator\anaconda3\lib\site-packages (from bokeh) (6.3.2)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\administrator\anaconda3\lib\site-packages (from Jinja2>=2.9->bokeh) (2.1.1)
Note: you may need to restart the kernel to use updated packages.
In [17]:
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource, HoverTool

# Call output_notebook to display plots within the JupyterLab notebook
output_notebook()

# Create a ColumnDataSource from your DataFrame
source = ColumnDataSource(df)

# Create a Bokeh figure
p = figure(title="Tanzania GDP Over Time", x_axis_label="Year", y_axis_label="GDP (Constant 2015 Prices)", width=800, height=400)

# Add a line glyph for GDP
p.line(x='Year', y='Pato la Taifa (GDP) kwa bei za soko', source=source, line_width=2, legend_label="GDP")

# Add a hover tool for interactive tooltips
hover = HoverTool()
hover.tooltips = [("Year", "@Year"), ("GDP", "@{Pato la Taifa (GDP) kwa bei za soko}{0,0.00}")]
p.add_tools(hover)

# Show the interactive plot within the JupyterLab notebook
show(p, notebook_handle=True)
Loading BokehJS ...
Out[17]:

<Bokeh Notebook handle for In[17]>

In [31]:
from sklearn.model_selection import train_test_split

# Select the features (sectors) for regression
features = df.columns[2:-1]  # Exclude 'Year' and 'Pato la Taifa (GDP) kwa bei za soko'

# Define the target variable (GDP)
target = 'Pato la Taifa (GDP) kwa bei za soko'

# Split the data into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42)
In [19]:
from sklearn.linear_model import LinearRegression

# Create a linear regression model
model = LinearRegression()

# Fit the model to the training data
model.fit(X_train, y_train)
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:767: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if not hasattr(array, "sparse") and array.dtypes.apply(is_sparse).any():
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:605: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:614: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:605: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:614: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
Out[19]:
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
In [20]:
from sklearn.metrics import mean_squared_error, r2_score

# Make predictions on the test set
y_pred = model.predict(X_test)

# Calculate Mean Squared Error and R-squared (coefficient of determination)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared (R2): {r2:.2f}")
Mean Squared Error: 0.00
R-squared (R2): 1.00
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:767: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if not hasattr(array, "sparse") and array.dtypes.apply(is_sparse).any():
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:605: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:614: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:605: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:614: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:605: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype):
C:\Users\Administrator\anaconda3\lib\site-packages\sklearn\utils\validation.py:614: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
  if is_sparse(pd_dtype) or not is_extension_array_dtype(pd_dtype):
In [22]:
import matplotlib.pyplot as plt

# Create a scatter plot of actual vs. predicted GDP values
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.7)
plt.title("Actual vs. Predicted GDP (Regression)")
plt.xlabel("Actual GDP")
plt.ylabel("Predicted GDP")
plt.grid(True)

# Add a diagonal line (y = x) for reference
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], linestyle='--', color='red', linewidth=2)

# Show the plot
plt.show()
In [23]:
import matplotlib.pyplot as plt

# Calculate the residuals
residuals = y_test - y_pred

# Create a residual plot
plt.figure(figsize=(8, 6))
plt.scatter(y_pred, residuals, alpha=0.7)
plt.title("Residual Plot")
plt.xlabel("Predicted GDP")
plt.ylabel("Residuals (Actual - Predicted GDP)")
plt.axhline(y=0, color='r', linestyle='--', linewidth=2)
plt.grid(True)

# Show the plot
plt.show()
In [24]:
pip install statsmodels
Requirement already satisfied: statsmodels in c:\users\administrator\anaconda3\lib\site-packages (0.13.5)
Requirement already satisfied: patsy>=0.5.2 in c:\users\administrator\anaconda3\lib\site-packages (from statsmodels) (0.5.3)
Requirement already satisfied: pandas>=0.25 in c:\users\administrator\anaconda3\lib\site-packages (from statsmodels) (2.1.1)
Requirement already satisfied: numpy>=1.22.3 in c:\users\administrator\anaconda3\lib\site-packages (from statsmodels) (1.23.5)
Requirement already satisfied: scipy>=1.3 in c:\users\administrator\anaconda3\lib\site-packages (from statsmodels) (1.10.0)
Requirement already satisfied: packaging>=21.3 in c:\users\administrator\anaconda3\lib\site-packages (from statsmodels) (22.0)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\administrator\anaconda3\lib\site-packages (from pandas>=0.25->statsmodels) (2.8.2)
Requirement already satisfied: tzdata>=2022.1 in c:\users\administrator\anaconda3\lib\site-packages (from pandas>=0.25->statsmodels) (2023.3)
Requirement already satisfied: pytz>=2020.1 in c:\users\administrator\anaconda3\lib\site-packages (from pandas>=0.25->statsmodels) (2022.7)
Requirement already satisfied: six in c:\users\administrator\anaconda3\lib\site-packages (from patsy>=0.5.2->statsmodels) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
In [25]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
In [26]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
In [27]:
import matplotlib.pyplot as plt

# Line plot for total GDP
plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Pato la Taifa (GDP) kwa bei za soko'], marker='o')
plt.title('Total GDP Over the Years (2012-2022)')
plt.xlabel('Year')
plt.ylabel('GDP (in billions)')
plt.grid(True)
plt.show()
In [32]:
correlation_matrix = df.corr()
# You can visualize the correlation matrix as a heatmap here.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[32], line 1
----> 1 correlation_matrix = df.corr()

File ~\anaconda3\lib\site-packages\pandas\core\frame.py:10707, in DataFrame.corr(self, method, min_periods, numeric_only)
  10705 cols = data.columns
  10706 idx = cols.copy()
> 10707 mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
  10709 if method == "pearson":
  10710     correl = libalgos.nancorr(mat, minp=min_periods)

File ~\anaconda3\lib\site-packages\pandas\core\frame.py:1892, in DataFrame.to_numpy(self, dtype, copy, na_value)
   1890 if dtype is not None:
   1891     dtype = np.dtype(dtype)
-> 1892 result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
   1893 if result.dtype is not dtype:
   1894     result = np.array(result, dtype=dtype, copy=False)

File ~\anaconda3\lib\site-packages\pandas\core\internals\managers.py:1656, in BlockManager.as_array(self, dtype, copy, na_value)
   1654         arr.flags.writeable = False
   1655 else:
-> 1656     arr = self._interleave(dtype=dtype, na_value=na_value)
   1657     # The underlying data was copied within _interleave, so no need
   1658     # to further copy if copy=True or setting na_value
   1660 if na_value is lib.no_default:

File ~\anaconda3\lib\site-packages\pandas\core\internals\managers.py:1715, in BlockManager._interleave(self, dtype, na_value)
   1713     else:
   1714         arr = blk.get_values(dtype)
-> 1715     result[rl.indexer] = arr
   1716     itemmask[rl.indexer] = 1
   1718 if not itemmask.all():

ValueError: could not convert string to float: 'Q1'
In [33]:
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Combine 'Year' and 'Quarter' columns to create a date column
df['Date'] = pd.to_datetime(df['Year'].astype(str) + df['Quarter'].str.replace('Q', ''), format='%Y%m')

# Set the date column as the index
df.set_index('Date', inplace=True)

# Select the GDP columns for analysis (excluding 'Year' and 'Quarter')
gdp_columns = df.columns[2:-1]

# Perform seasonal decomposition for each GDP component
plt.figure(figsize=(12, 8))
for col in gdp_columns:
    result = seasonal_decompose(df[col], model='additive', period=4)  # You can adjust the period as needed
    result.plot()
    plt.title(f'Seasonal Decomposition of {col}')
    plt.show()
<Figure size 1200x800 with 0 Axes>
In [34]:
import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset and prepare the data if not done already

# Select a specific GDP component for trend analysis (e.g., 'kilimo')
selected_component = 'kilimo'

# Calculate the 4-quarter moving average (SMA)
df['SMA_4Q'] = df[selected_component].rolling(window=4).mean()

# Plot the original data and the SMA
plt.figure(figsize=(12, 6))
plt.plot(df.index, df[selected_component], label=f'Original {selected_component}')
plt.plot(df.index, df['SMA_4Q'], label=f'4-Quarter SMA of {selected_component}')
plt.title(f'Trend Analysis of {selected_component} (2012-2022)')
plt.xlabel('Year')
plt.ylabel('GDP Value')
plt.legend()
plt.grid(True)
plt.show()
In [35]:
import pandas as pd
import statsmodels.api as sm

# Load the dataset and prepare the data if not done already

# Specify the independent variable (predictor)
independent_variable = 'Elimu'  # Replace with the variable you want to use

# Define the dependent variable (GDP)
dependent_variable = 'Pato la Taifa (GDP) kwa bei za soko'

# Add a constant (intercept) to the independent variable
X = sm.add_constant(df[independent_variable])

# Fit the linear regression model
model = sm.OLS(df[dependent_variable], X).fit()

# Print the model summary
print(model.summary())
                                     OLS Regression Results                                    
===============================================================================================
Dep. Variable:     Pato la Taifa (GDP) kwa bei za soko   R-squared:                       0.907
Model:                                             OLS   Adj. R-squared:                  0.905
Method:                                  Least Squares   F-statistic:                     410.6
Date:                                 Fri, 06 Oct 2023   Prob (F-statistic):           2.66e-23
Time:                                         16:21:26   Log-Likelihood:                -692.15
No. Observations:                                   44   AIC:                             1388.
Df Residuals:                                       42   BIC:                             1392.
Df Model:                                            1                                         
Covariance Type:                             nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const       3.895e+06   1.18e+06      3.305      0.002    1.52e+06    6.27e+06
Elimu         33.2101      1.639     20.264      0.000      29.903      36.517
==============================================================================
Omnibus:                        3.456   Durbin-Watson:                   2.781
Prob(Omnibus):                  0.178   Jarque-Bera (JB):                1.730
Skew:                          -0.142   Prob(JB):                        0.421
Kurtosis:                       2.071   Cond. No.                     3.35e+06
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 3.35e+06. This might indicate that there are
strong multicollinearity or other numerical problems.
In [ ]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Select the GDP components for the Radar Chart (excluding 'Year' and 'Quarter')
gdp_components = df.columns[2:-1].tolist()

# Select a specific year for visualization (e.g., 2022)
selected_year = 2022

# Filter the dataset for the selected year
data_for_radar = df[df['Year'] == selected_year][gdp_components]

# Define the categories (GDP components) and values for the Radar Chart
categories = data_for_radar.columns
values = data_for_radar.iloc[0].values

# Number of categories (GDP components)
num_categories = len(categories)

# Create a list of angles (radians) for the Radar Chart
angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()
angles += angles[:1]  # Close the plot by adding the first angle to the end

# Create the Radar Chart
plt.figure(figsize=(8, 8))
ax = plt.subplot(111, polar=True)
plt.xticks(angles[:-1], categories, fontsize=12)
plt.yticks(np.arange(0, max(values) + 1, 5), fontsize=10)
ax.fill(angles, values, 'b', alpha=0.1)

# Add a title
plt.title(f'Radar Chart of GDP Components for {selected_year}', size=16, color='blue')

# Show the Radar Chart
plt.show()
In [ ]:
import pandas as pd
import plotly.express as px

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Select the columns relevant for the TreeMap (e.g., GDP components and their values for a specific year)
selected_year = 2022  # Replace with the year you want to visualize
data_for_treemap = df[df['Year'] == selected_year][['Year'] + df.columns[2:-1].tolist()]

# Melt the data for use in the TreeMap
data_for_treemap = pd.melt(data_for_treemap, id_vars=['Year'], var_name='GDP Component', value_name='GDP Value')

# Create the TreeMap
fig = px.treemap(data_for_treemap, 
                 path=['Year', 'GDP Component'], 
                 values='GDP Value',
                 color='GDP Value',
                 color_continuous_scale='RdBu',
                 title=f'TreeMap of GDP Components for {selected_year}')

# Show the TreeMap
fig.show()
In [ ]:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Select three GDP components for the 3D scatter plot
component1 = 'kilimo'  # Replace with the first component
component2 = 'Viwanda'  # Replace with the second component
component3 = 'Uchukuzi na Uhifadhi Mizigo'  # Replace with the third component

# Create a figure and a 3D axis
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Scatter plot with three GDP components
ax.scatter(df[component1], df[component2], df[component3], c=df['Year'], cmap='viridis', s=50, alpha=0.6)

# Set labels and title
ax.set_xlabel(component1)
ax.set_ylabel(component2)
ax.set_zlabel(component3)
ax.set_title(f'3D Scatter Plot of GDP Components (2012-2022)')

# Show the plot
plt.show()
In [29]:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Select three GDP components for the 3D scatter plot
component1 = 'Ujenzi'  # Replace with the first component
component2 = 'Umeme'  # Replace with the second component
component3 = 'Viwanda'  # Replace with the third component

# Create a figure and a 3D axis
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Scatter plot with three GDP components
ax.scatter(df[component1], df[component2], df[component3], c=df['Year'], cmap='viridis', s=50, alpha=0.6)

# Set labels and title
ax.set_xlabel(component1)
ax.set_ylabel(component2)
ax.set_zlabel(component3)
ax.set_title(f'3D Scatter Plot of GDP Components (2012-2022)')

# Show the plot
plt.show()
In [30]:
import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset
file_path = "TZ_GDP_2012_2022_Constant_2015_prices.xlsx"
df = pd.read_excel(file_path)

# Extract the GDP components (excluding 'Year' and 'Quarter')
gdp_components = df.columns[2:-1]

# Create a box plot for each GDP component for each year
plt.figure(figsize=(12, 8))
for component in gdp_components:
    plt.boxplot([df[df['Year'] == year][component] for year in df['Year'].unique()], labels=df['Year'].unique())
    plt.title(f'Box Plot of {component} by Year (2012-2022)')
    plt.xlabel('Year')
    plt.ylabel(component)
    plt.xticks(rotation=45)
    plt.show()
In [ ]: