# Import all required libraries
import warnings
from prettytable import PrettyTable
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
import plotly.figure_factory as ff
import altair as alt
alt.renderers.enable('default')
warnings.filterwarnings('ignore')
import_data = pd.read_csv("D:/Datasets/2018-2010_import.csv")
export_data = pd.read_csv("D:/Datasets/2018-2010_export.csv")
styles = [
dict(selector="tr:hover", props=[("background", "#f4f4f4")]),
dict(selector="th",
props=[("color", "#fff"), ("border", "1px solid #eee"),
("padding", "12px 35px"), ("border-collapse", "collapse"),
("background", "#00cccc"), ("font-size", "18px")]),
dict(selector="caption",
props=[
("caption-side", "top"),
("font-size", "25px"),
("border", "1px solid #eee"),
("padding", "12px 35px"),
]),
]
import_data.head().style.set_table_styles(styles).highlight_null(
null_color='#FF8C00').set_caption('Import Data')
HSCode | Commodity | value | country | year | |
---|---|---|---|---|---|
0 | 5 | PRODUCTS OF ANIMAL ORIGIN, NOT ELSEWHERE SPECIFIED OR INCLUDED. | 0.000000 | AFGHANISTAN TIS | 2018 |
1 | 7 | EDIBLE VEGETABLES AND CERTAIN ROOTS AND TUBERS. | 12.380000 | AFGHANISTAN TIS | 2018 |
2 | 8 | EDIBLE FRUIT AND NUTS; PEEL OR CITRUS FRUIT OR MELONS. | 268.600000 | AFGHANISTAN TIS | 2018 |
3 | 9 | COFFEE, TEA, MATE AND SPICES. | 35.480000 | AFGHANISTAN TIS | 2018 |
4 | 11 | PRODUCTS OF THE MILLING INDUSTRY; MALT; STARCHES; INULIN; WHEAT GLUTEN. | nan | AFGHANISTAN TIS | 2018 |
styles = [
dict(selector="tr:hover", props=[("background", "#f4f4f4")]),
dict(selector="th",
props=[("color", "#000000"), ("border", "1px solid #eee"),
("padding", "12px 35px"), ("border-collapse", "collapse"),
("background", "#FF7F50"), ("font-size", "18px")]),
dict(selector="caption",
props=[
("caption-side", "top"),
("font-size", "25px"),
("border", "1px solid #eee"),
("padding", "12px 35px"),
]),
]
import_data.describe().style.set_table_styles(styles).highlight_min(
color='#008B8B').set_caption(
"Short Summary of Numerical Varaibles of Import Data")
HSCode | value | year | |
---|---|---|---|
count | 76124.000000 | 64536.000000 | 76124.000000 |
mean | 53.905023 | 62.361425 | 2014.018299 |
std | 27.546852 | 666.327177 | 2.579385 |
min | 1.000000 | 0.000000 | 2010.000000 |
25% | 30.000000 | 0.030000 | 2012.000000 |
50% | 54.000000 | 0.370000 | 2014.000000 |
75% | 78.000000 | 4.780000 | 2016.000000 |
max | 99.000000 | 32781.570000 | 2018.000000 |
styles = [
dict(selector="tr:hover", props=[("background", "#f4f4f4")]),
dict(selector="th",
props=[("color", "#000000"), ("border", "1px solid #eee"),
("padding", "12px 35px"), ("border-collapse", "collapse"),
("background", "#FFD700"), ("font-size", "18px")]),
dict(selector="caption",
props=[
("caption-side", "top"),
("font-size", "25px"),
("border", "1px solid #eee"),
("padding", "12px 35px"),
]),
]
export_data.describe().style.set_table_styles(styles).highlight_min(
color='#E9967A').set_caption(
"Short Summary of Numerical Varaibles of Export Data")
HSCode | value | year | |
---|---|---|---|
count | 137023.000000 | 122985.000000 | 137023.000000 |
mean | 51.330302 | 21.567829 | 2014.056304 |
std | 28.018026 | 229.701279 | 2.580160 |
min | 1.000000 | 0.000000 | 2010.000000 |
25% | 28.000000 | 0.030000 | 2012.000000 |
50% | 52.000000 | 0.360000 | 2014.000000 |
75% | 74.000000 | 3.770000 | 2016.000000 |
max | 99.000000 | 19805.170000 | 2018.000000 |
# Check Number of Rows where value is zero
print("No of Rows in Import Data where value is zero =",
len(import_data[import_data['value'] == 0]))
print("No of Rows in Export Data where value is zero =",
len(export_data[export_data['value'] == 0]))
No of Rows in Import Data where value is zero = 8596 No of Rows in Export Data where value is zero = 15860
# Drop Missing Values
import_data.dropna(inplace=True)
export_data.dropna(inplace=True)
# Fix some long Commodity names
dic = {
'PRODUCTS OF ANIMAL ORIGIN, NOT ELSEWHERE SPECIFIED OR INCLUDED.':
'ANIMAL PRODUCTS',
'NUCLEAR REACTORS, BOILERS, MACHINERY AND MECHANICAL APPLIANCES; PARTS THEREOF.':
'NUCLEAR EQUIPMENT',
'OPTICAL, PHOTOGRAPHIC CINEMATOGRAPHIC MEASURING, CHECKING PRECISION, MEDICAL OR SURGICAL INST. AND APPARATUS PARTS AND ACCESSORIES THEREOF;':
'OPTICAL INSTRUMENTS',
'FURNITURE; BEDDING, MATTRESSES, MATTRESS SUPPORTS, CUSHIONS AND SIMILAR STUFFED FURNISHING; LAMPS AND LIGHTING FITTINGS NOT ELSEWHERE SPECIFIED OR INC':
'FURNITURE',
'ELECTRICAL MACHINERY AND EQUIPMENT AND PARTS THEREOF; SOUND RECORDERS AND REPRODUCERS, TELEVISION IMAGE AND SOUND RECORDERS AND REPRODUCERS,AND PARTS.':
'ELECTRICAL MACHINERY',
'RAILWAY OR TRAMWAY LOCOMOTIVES, ROLLING-STOCK AND PARTS THEREOF; RAILWAY OR TRAMWAY TRACK FIXTURES AND FITTINGS AND PARTS THEREOF; MECHANICAL':
'RAILWAY MACHINERY',
'TANNING OR DYEING EXTRACTS; TANNINS AND THEIR DERI. DYES, PIGMENTS AND OTHER COLOURING MATTER; PAINTS AND VER; PUTTY AND OTHER MASTICS; INKS.':
'TANNING EXTRACTS',
'PREPARATIONS OF MEAT, OF FISH OR OF CRUSTACEANS, MOLLUSCS OR OTHER AQUATIC INVERTEBRATES':
'PREPARATIONS OF NONVEG FOOD',
'EXPLOSIVES; PYROTECHNIC PRODUCTS; MATCHES; PYROPHORIC ALLOYS; CERTAIN COMBUSTIBLE PREPARATIONS.':
'EXPLOSIVES',
'VEGETABLE PLAITING MATERIALS; VEGETABLE PRODUCTS NOT ELSEWHERE SPECIFIED OR INCLUDED.':
'VEGETABLE PLAITING MATERIALS',
'ARTICLES OF APPAREL AND CLOTHING ACCESSORIES, NOT KNITTED OR CROCHETED.':
'CLOTHING',
'OTHER MADE UP TEXTILE ARTICLES; SETS; WORN CLOTHING AND WORN TEXTILE ARTICLES; RAGS':
'TEXTILES',
'VEHICLES OTHER THAN RAILWAY OR TRAMWAY ROLLING STOCK, AND PARTS AND ACCESSORIES THEREOF.':
'SPARE PARTS'
}
import_data['Commodity'] = import_data['Commodity'].replace(dic)
export_data['Commodity'] = export_data['Commodity'].replace(dic)
# Fixing Some Country Names
import_data.country = import_data.country.replace({
'U K': 'UK',
'U S A': 'USA',
'AFGHANISTAN TIS': 'AFGHANISTAN',
'BAHARAIN IS': 'BAHARAIN',
'BANGLADESH PR': 'BANGLADESH',
'BOSNIA-HRZGOVIN': 'BOSNIA',
'BURKINA FASO': 'BURKINA',
'CHINA P RP': 'CHINA'
})
export_data.country = export_data.country.replace({
'U K': 'UK',
'U S A': 'USA',
'AFGHANISTAN TIS': 'AFGHANISTAN',
'BAHARAIN IS': 'BAHARAIN',
'BANGLADESH PR': 'BANGLADESH',
'BOSNIA-HRZGOVIN': 'BOSNIA',
'BURKINA FASO': 'BURKINA',
'CHINA P RP': 'CHINA'
})
# Univariate Analysis of HSCode
if len(
set(list(import_data.HSCode.unique())) -
set(list(export_data.HSCode.unique()))) == 0:
print(
"HSCode Values for Import Data and Export Data are different which should happen."
)
else:
print(
"Some HSCode Values are used for both Import and Export which should not happen"
)
HSCode Values for Import Data and Export Data are different which should happen.
# Univariate Analysis of Commodities
unique_import_commodities = import_data.Commodity.unique().tolist()
unique_export_commodities = export_data.Commodity.unique().tolist()
print("No of Unique Commodities Imported from 2010 to 2018 =",
len(unique_import_commodities))
print("No of Unique Commodities Exported from 2010 to 2018 =",
len(unique_export_commodities))
import_commodities = import_data.Commodity.unique()
import_commodities_names = list(
import_data.Commodity.value_counts().head(8).index)
import_commodities_values = list(
import_data.Commodity.value_counts().head(8).values)
export_commodities_names = list(
export_data.Commodity.value_counts().head(8).index)
export_commodities_values = list(
export_data.Commodity.value_counts().head(8).values)
table = PrettyTable([' Top 10 Import Commodity', 'Count'])
for name, value in zip(import_commodities_names, import_commodities_values):
table.add_row([name, value])
print(table)
table = PrettyTable([' Top 10 Export Commodity', 'Count'])
for name, value in zip(export_commodities_names, export_commodities_values):
table.add_row([name, value])
print(table)
No of Unique Commodities Imported from 2010 to 2018 = 98 No of Unique Commodities Exported from 2010 to 2018 = 98 +---------------------------------+-------+ | Top 10 Import Commodity | Count | +---------------------------------+-------+ | ELECTRICAL MACHINERY | 1576 | | NUCLEAR EQUIPMENT | 1504 | | IRON AND STEEL | 1378 | | PLASTIC AND ARTICLES THEREOF. | 1295 | | ALUMINIUM AND ARTICLES THEREOF. | 1289 | | OPTICAL INSTRUMENTS | 1203 | | COPPER AND ARTICLES THEREOF. | 1117 | | ARTICLES OF IRON OR STEEL | 1099 | +---------------------------------+-------+ +-------------------------------+-------+ | Top 10 Export Commodity | Count | +-------------------------------+-------+ | NUCLEAR EQUIPMENT | 1882 | | ELECTRICAL MACHINERY | 1878 | | PHARMACEUTICAL PRODUCTS | 1876 | | OPTICAL INSTRUMENTS | 1849 | | CLOTHING | 1817 | | PLASTIC AND ARTICLES THEREOF. | 1809 | | ARTICLES OF IRON OR STEEL | 1797 | | TEXTILES | 1791 | +-------------------------------+-------+
# Univaraite Analysis of Country
import_countries = import_data.country.unique()
export_countries = export_data.country.unique()
print("No of Countries from which Commodities are Imported =",
len(import_countries))
print("No of Countries to which Commodities are Exported =",
len(export_countries))
import_countries_names = list(
import_data.country.value_counts().head(8).index) + ['Rest All']
import_countries_values = list(
import_data.country.value_counts().head(8).values) + [
len(import_data) - import_data.country.value_counts().head(8).sum()
]
export_countries_names = list(
export_data.country.value_counts().head(8).index) + ['Rest All']
export_countries_values = list(
export_data.country.value_counts().head(8).values) + [
len(export_data) - export_data.country.value_counts().head(8).sum()
]
fig = make_subplots(rows=1,
cols=2,
specs=[[{
'type': 'domain'
}, {
'type': 'domain'
}]])
fig.add_trace(
go.Pie(hoverinfo="label+percent+name",
labels=import_countries_names,
values=import_countries_values,
hole=0.3,
rotation=120), 1, 1)
fig.add_trace(
go.Pie(hoverinfo="label+percent+name",
labels=export_countries_names,
values=export_countries_values,
hole=0.3,
rotation=180), 1, 2)
fig.update_layout(
title_text='Side by Side Donut Chart of Top 8 Import and Export Countries',
template='plotly_dark',
annotations=[
dict(x=0.18, y=0.5, text='Import', font_size=20, showarrow=False),
dict(x=0.82, y=0.5, text='Export', font_size=20, showarrow=False)
])
fig.show()
No of Countries from which Commodities are Imported = 241 No of Countries to which Commodities are Exported = 248
# Univariate Analysis of Value
fig = make_subplots(cols=2,
subplot_titles=[
f'Histogram of Import Values',
f'Histogram of Export Values'
])
fig.add_trace(go.Histogram(
x=import_data[import_data['value'] != 0]['value'],
marker=dict(color='#00FFFF'),
),
row=1,
col=2)
fig.add_trace(go.Histogram(x=export_data[export_data['value'] != 0]['value'],
marker_color='Aquamarine'),
row=1,
col=1)
fig.update_layout(title_text=f"Side by Side Histogram and Box plot of Value")
fig.update_layout(showlegend=False, template='plotly_dark')
fig['layout']['xaxis'].update(title_text='Value in Millions')
fig['layout']['yaxis'].update(title_text="Frequency")
fig['layout']['yaxis2'].update(title_text="Frequency")
fig['layout']['xaxis2'].update(title_text='Value in Millions')
fig.show()
#Univariate Analysis of Year
year = import_data.year.value_counts().head(1).index[0]
print("The Year Which has most Import Transactions =", year)
year = export_data.year.value_counts().head(1).index[0]
print("The Year Which has most Export Transactions =", year)
The Year Which has most Import Transactions = 2015 The Year Which has most Export Transactions = 2018
Observations of Univariate Analysis:
HS-Codes are different for export and import.
Some Commodities are imported as well as exported.
Alot of values of Value of trade (in millions) in near 0 and has outliers.
# Calculate Growth for Import and Export Data and deficit
i = import_data.groupby('year', as_index=False).agg({
"value": "sum"
}).rename(columns={'value': 'Import'})
e = export_data.groupby('year', as_index=False).agg({
"value": "sum"
}).rename(columns={'value': 'Export'})
df = pd.merge(i, e, on='year')
df['Growth in Import'] = df.set_index('year')['Import'].pct_change().values
df['Growth in Export'] = df.set_index('year')['Export'].pct_change().values
df['Deficit'] = df['Export'] - df['Import']
df['Growth in Import'].fillna(0, inplace=True)
df['Growth in Export'].fillna(0, inplace=True)
fig = go.Figure(data=([
go.Bar(x=df.year,
y=df.Import,
name='Import',
marker={"color": "#50D3E3"},
textangle=0,
textfont={'size': 12},
texttemplate='%{y:.2s}',
textposition='outside'),
go.Bar(x=df.year,
y=df.Export,
marker={"color": "#FF3900"},
name="Export",
textangle=0,
textfont={'size': 12},
texttemplate='%{y:.2s}',
textposition='outside')
]))
fig.update_layout(
title="Grouped Bar Plot of Import and Export from 2010 to 2018",
xaxis_title="Year",
yaxis_title="Value",
template='seaborn')
fig.show()
# Scatter Plot of Growth Rate in Import and Export from 2010 to 2018
fig = go.Figure(data=([
go.Scatter(x=df.year,
y=df['Growth in Import'],
name='Import',
marker={"color": "#99CB00"}),
go.Scatter(x=df.year,
y=df['Growth in Export'],
marker={"color": "#F3B524"},
name="Export")
]))
fig.update_layout(
title="Scatter Plot of Growth Rate in Import and Export from 2010 to 2018",
xaxis_title="Year",
yaxis_title="Rate",
template='plotly_dark')
fig.show()
# Scatter Plot of Deficit from 2010 to 2018
fig = go.Figure(data=([
go.Scatter(
x=df.year,
y=df['Deficit'],
name='Deficit',
marker={"color": "#008B8B"},
)
]))
fig.update_layout(title="Scatter Plot of Deficit from 2010 to 2018",
xaxis_title="Year",
yaxis_title="Deficit",
template='plotly_dark')
fig.show()
Observations from above 3 plots:-
The total value of Import in a year has always been greather then Export.
2011 has highest Import and Export rate.2015 had lowest Export rate.2014 had equal Import and Export rates. 2016 was best year because Export was much higher and Import was also very less.
Deficit decreased from 2010 to 2012, when up from 2012 to 2016 and again decreased from 2016 to 2012.
Interactive Bar plot of Top 5 Import Commodities with year as slider
# Interactive Bar plot of Top 5 Import Countries with year as slider
top_commodities = import_data.groupby(['year', 'Commodity'],
as_index=False).agg({'value': 'sum'})
top_2010 = top_commodities[top_commodities.year == 2010].sort_values(
'value', ascending=False).head(5)
top_2011 = top_commodities[top_commodities.year == 2011].sort_values(
'value', ascending=False).head(5)
top_2012 = top_commodities[top_commodities.year == 2012].sort_values(
'value', ascending=False).head(5)
top_2013 = top_commodities[top_commodities.year == 2013].sort_values(
'value', ascending=False).head(5)
top_2014 = top_commodities[top_commodities.year == 2014].sort_values(
'value', ascending=False).head(5)
top_2015 = top_commodities[top_commodities.year == 2015].sort_values(
'value', ascending=False).head(5)
top_2016 = top_commodities[top_commodities.year == 2016].sort_values(
'value', ascending=False).head(5)
top_2017 = top_commodities[top_commodities.year == 2017].sort_values(
'value', ascending=False).head(5)
top_2018 = top_commodities[top_commodities.year == 2018].sort_values(
'value', ascending=False).head(5)
top_commodities = pd.concat([
top_2010, top_2011, top_2012, top_2013, top_2014, top_2015, top_2016,
top_2017, top_2018
])
d = {
"ORGANIC CHEMICALS":
'Organic',
'MINERAL FUELS, MINERAL OILS AND PRODUCTS OF THEIR DISTILLATION; BITUMINOUS SUBSTANCES; MINERAL WAXES.':
'Mineral',
'NATURAL OR CULTURED PEARLS,PRECIOUS OR SEMIPRECIOUS STONES,PRE.METALS,CLAD WITH PRE.METAL AND ARTCLS THEREOF;IMIT.JEWLRY;COIN.':
'Natural',
'NUCLEAR EQUIPMENT':
'Nuclear',
'ELECTRICAL MACHINERY':
'Electrical'
}
top_commodities['Commodity_Name'] = top_commodities['Commodity'].map(d)
colors = {
'Organic': '#FF800C',
'Mineral': '#4255FF',
'Nuclear': '#FFF312',
'Natural': '#A2F312',
'Electrical': '#FF075B'
}
top_commodities['Color'] = top_commodities['Commodity_Name'].map(colors)
fig = px.bar(
data_frame=top_commodities,
x='Commodity_Name',
y='value',
animation_frame='year',
hover_name='Commodity',
title=
"Interactive Bar Plot of Top 5 Import Commodities with year as slider",
color='Color')
fig.update_layout(template='plotly_dark',
showlegend=False,
yaxis_title_text='USD (in Millions)')
fig.show()
Interactive Bar Plot of Top 5 Export Commodities with year as slider
# Interactive Bar Plot of Top 5 Export Commodities with year as slider
top_commodities = export_data.groupby(['year', 'Commodity'],
as_index=False).agg({'value': 'sum'})
top_2010 = top_commodities[top_commodities.year == 2010].sort_values(
'value', ascending=False).head(5)
top_2011 = top_commodities[top_commodities.year == 2011].sort_values(
'value', ascending=False).head(5)
top_2012 = top_commodities[top_commodities.year == 2012].sort_values(
'value', ascending=False).head(5)
top_2013 = top_commodities[top_commodities.year == 2013].sort_values(
'value', ascending=False).head(5)
top_2014 = top_commodities[top_commodities.year == 2014].sort_values(
'value', ascending=False).head(5)
top_2015 = top_commodities[top_commodities.year == 2015].sort_values(
'value', ascending=False).head(5)
top_2016 = top_commodities[top_commodities.year == 2016].sort_values(
'value', ascending=False).head(5)
top_2017 = top_commodities[top_commodities.year == 2017].sort_values(
'value', ascending=False).head(5)
top_2018 = top_commodities[top_commodities.year == 2018].sort_values(
'value', ascending=False).head(5)
top_commodities = pd.concat([
top_2010, top_2011, top_2012, top_2013, top_2014, top_2015, top_2016,
top_2017, top_2018
])
d = {
'NATURAL OR CULTURED PEARLS,PRECIOUS OR SEMIPRECIOUS STONES,PRE.METALS,CLAD WITH PRE.METAL AND ARTCLS THEREOF;IMIT.JEWLRY;COIN.':
'Jewelry',
'MINERAL FUELS, MINERAL OILS AND PRODUCTS OF THEIR DISTILLATION; BITUMINOUS SUBSTANCES; MINERAL WAXES.':
'Mineral',
'MISCELLANEOUS GOODS.':
'MISCELLANEOUS GOODS.',
'ELECTRICAL MACHINERY':
'Electrical Machinery',
'SPARE PARTS':
'Vehicle Parts',
'ORGANIC CHEMICALS':
'Organic Chemicals',
'NUCLEAR EQUIPMENT':
'Nuclear Reactors',
'PHARMACEUTICAL PRODUCTS':
'PHARMACEUTICAL PRODUCTS'
}
top_commodities['Commodity_Name'] = top_commodities['Commodity'].map(d)
colors = {
'Jewelry': '#FF800C',
'Mineral': '#4255FF',
'MISCELLANEOUS GOODS.': '#FFF312',
'Electrical Machinery': '#A2F312',
'Vehicle Parts': '#FF075B',
'Organic Chemicals': "#6EC9CB",
'Nuclear Reactors': '#B95C46',
'PHARMACEUTICAL PRODUCTS': '#B9F846'
}
top_commodities['Color'] = top_commodities['Commodity_Name'].map(colors)
fig = px.bar(
data_frame=top_commodities,
x='Commodity_Name',
y='value',
animation_frame='year',
hover_name='Commodity',
title=
"Interactive Bar Plot of Top 5 Export Commodities with year as slider",
color='Color')
fig.update_layout(template='plotly_dark',
showlegend=False,
yaxis_title_text='USD (in Millions)')
fig.show()
# Trend of Some Commodities
commodities = [
'ELECTRICAL MACHINERY',
'NUCLEAR EQUIPMENT',
'PLASTIC AND ARTICLES THEREOF.',
]
i = 0
fig, ax = plt.subplots(nrows=3, figsize=(15, 9))
df_i = import_data[import_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
df_e = export_data[export_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
ax[i].plot(df_i['year'],
df_i['value'],
color='red',
linewidth=3,
label='Import')
ax[i].plot(df_e['year'],
df_e['value'],
color='green',
linewidth=3,
label='Export')
ax[i].set_xlabel('Year', fontsize=15)
ax[i].set_ylabel('USD (in Millions)', fontsize=15)
ax[i].set_title(
'Trend of Import & Export of Electrical Machinery from 2010-2018 ',
fontsize=20)
ax[i].legend(loc='upper right')
ax[i].grid(True)
i += 1
df_i = import_data[import_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
df_e = export_data[export_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
ax[i].plot(df_i['year'],
df_i['value'],
color='red',
linewidth=3,
label='Import')
ax[i].plot(df_e['year'],
df_e['value'],
color='green',
linewidth=3,
label='Export')
ax[i].set_xlabel('Year', fontsize=15)
ax[i].set_ylabel('USD (in Millions)', fontsize=15)
ax[i].set_title(
'Trend of Import & Export of Electrical Machinery from 2010-2018 ',
fontsize=20)
ax[i].legend(loc='upper right')
ax[i].grid(True)
i += 1
df_i = import_data[import_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
df_e = export_data[export_data.Commodity == commodities[i]].groupby(
'year', as_index=False).agg({'value': np.sum})
ax[i].plot(df_i['year'],
df_i['value'],
color='red',
linewidth=3,
label='Import')
ax[i].plot(df_e['year'],
df_e['value'],
color='green',
linewidth=3,
label='Export')
ax[i].set_xlabel('Year', fontsize=15)
ax[i].set_ylabel('USD (in Millions)', fontsize=15)
ax[i].set_title(
'Trend of Import & Export of Electrical Machinery from 2010-2018 ',
fontsize=20)
ax[i].legend(loc='upper right')
ax[i].grid(True)
plt.subplots_adjust(hspace=0.6)
plt.show()
Country Wise Analysis
# Bar Chart of Import and Export Countries
df = import_data.groupby('country', as_index=False).agg({'value': 'sum'})
df = df.sort_values(by='value', ascending=False).head(10)
fig = go.Figure()
fig.add_trace(
go.Bar(x=df['value'],
y=df['country'],
marker_color=px.colors.qualitative.Pastel[:10],
hovertemplate='x: %{x}',
orientation='h',
name='Import',
texttemplate="%{x:$.2f}",
textposition='inside'))
fig.update_layout(title_text='Import of India Top 10 Countries(2010-2018)',
title_font_size=25,
xaxis_title_text='USD (in Millions)',
yaxis_title_text='Country',
xaxis_title_font_size=15,
yaxis_title_font_size=15,
showlegend=False,
template='ggplot2')
fig.show()
df = export_data.groupby('country', as_index=False).agg({'value': 'sum'})
df = df.sort_values(by='value', ascending=False).head(10)
fig = go.Figure()
fig.add_trace(
go.Bar(x=df['value'],
y=df['country'],
marker_color=px.colors.qualitative.Pastel[:10],
hovertemplate='x: %{x}',
orientation='h',
name='Import',
texttemplate="%{x:$.2f}",
textposition='inside'))
fig.update_layout(title_text='Export of India Top 10 Countries(2010-2018)',
title_font_size=25,
xaxis_title_text='USD (in Millions)',
yaxis_title_text='Country',
xaxis_title_font_size=15,
yaxis_title_font_size=15,
showlegend=False,
template='ggplot2')
fig.show()
Observations
China has biggest market in india followed by UAE, Saudi Arabia and USA.
For India, USA is biggest importer followed by UAE and China Republic.
# Interactive plot of Import from country through years.
unique_countries = sorted(list(import_data.country.unique()))
default = unique_countries[0]
fig = go.Figure()
buttons, countries = [], []
for country in unique_countries:
df = import_data[import_data.country == country]
new_df = df.groupby('year', as_index=False).agg({'value': np.sum})
fig.add_trace(
go.Scatter(x=new_df['year'],
y=new_df['value'],
visible=(country == default),
mode='markers+lines',
name=country,
marker_color='#00FFFF'))
countries.extend([country])
for country in unique_countries:
buttons.append(
dict(method='update',
label=country,
args=[{
'visible': [country == r for r in countries]
}]))
fig.update_layout(
showlegend=False,
title_text='Line Chart of Import Value from Countries throughout Years.',
title_font_color='#FF8C00',
title_font_size=25,
updatemenus=[{
"buttons": buttons,
"font": {
'color': '#000080'
},
"direction": "down",
"active": unique_countries.index(default),
"showactive": True,
"x": 1,
"y": 1.08,
'xanchor': 'right',
'bgcolor': '#DEB887'
}],
template='plotly_dark')
fig.update_layout({
'xaxis': {
'title': {
'text': 'Years',
'font': {
'color': '#00FA9A',
'size': 20
}
}
}
})
fig.update_layout({
'yaxis': {
'title': {
'text': 'USD (in Millions)',
'font': {
'color': '#00FA9A',
'size': 20
}
}
}
})
# fig['layout']['xaxis'].update(title_text='Years',title_font_color='#00FA9A',title_font_size=20)
# fig['layout']['yaxis'].update(title_text='Value',title_font_color='#00FA9A',title_font_size=20)
fig.show()
# Interactive plot of Export to countries through years.
unique_countries = sorted(list(export_data.country.unique()))
default = unique_countries[0]
fig = go.Figure()
buttons, countries = [], []
for country in unique_countries:
df = export_data[export_data.country == country]
new_df = df.groupby('year', as_index=False).agg({'value': np.sum})
fig.add_trace(
go.Scatter(x=new_df['year'],
y=new_df['value'],
visible=(country == default),
mode='markers+lines',
name=country,
marker_color='#00FFFF'))
countries.extend([country])
for country in unique_countries:
buttons.append(
dict(method='update',
label=country,
args=[{
'visible': [country == r for r in countries]
}]))
fig.update_layout(
showlegend=False,
title_text='Line Chart of Export Value to Countries throughout Years.',
title_font_color='#FF8C00',
title_font_size=25,
updatemenus=[{
"buttons": buttons,
"font": {
'color': '#000080'
},
"direction": "down",
"active": unique_countries.index(default),
"showactive": True,
"x": 1,
"y": 1.08,
'xanchor': 'right',
'bgcolor': '#DEB887'
}],
template='plotly_dark')
fig['layout']['xaxis'].update(title_text='Years',
title_font_color='#00FA9A',
title_font_size=20)
fig['layout']['yaxis'].update(title_text='USD (in Millions)',
title_font_color='#00FA9A',
title_font_size=20)
fig.show()
List Of indian HS Classification is based on HS Code used in actual Shipment Data: http://www.cybex.in/HS-Codes/Default.aspx
def get_productype(x):
x
if (x >= 1) & (x <= 5):
return 'Animal Products'
elif (x >= 6) & (x <= 14):
return 'Vegetable Products'
elif (x == 15):
return 'Animal Fats & Oils'
elif (x >= 16) & (x <= 24):
return 'Food Stuffs'
elif (x >= 25) & (x <= 27):
return 'Mineral Products'
elif (x >= 28) & (x <= 38):
return 'Chemical Products'
elif (x >= 39) & (x <= 40):
return 'Plastics'
elif (x >= 41) & (x <= 43):
return 'Raw Hides and Skins'
elif (x >= 44) & (x <= 46):
return 'Wood'
elif (x >= 47) & (x <= 49):
return 'Pulp of Wood'
elif (x >= 50) & (x <= 63):
return 'Textiles'
elif (x >= 64) & (x <= 67):
return 'Foot Wear'
elif (x >= 68) & (x <= 70):
return 'Articles of Stone,Plaster'
elif (x == 71):
return 'Natural Pearls'
elif (x >= 72) & (x <= 83):
return 'Base Metals'
elif (x >= 84) & (x <= 85):
return 'Machinery Appliance'
elif (x >= 86) & (x <= 89):
return 'Vehicles'
elif (x >= 90) & (x <= 92):
return 'Optical'
elif (x == 93):
return 'Ammunition'
elif (x >= 94) & (x <= 96):
return 'Manafactured Articles'
else:
'Work of Art'
import_data['Commodity_type'] = import_data['HSCode'].apply(
lambda x: get_productype(x))
export_data['Commodity_type'] = export_data['HSCode'].apply(
lambda x: get_productype(x))
import_commoditytype_names, import_commoditytype_values = import_data[
'Commodity_type'].value_counts(
).index, import_data['Commodity_type'].value_counts().values
export_commoditytype_names, export_commoditytype_values = export_data[
'Commodity_type'].value_counts(
).index, export_data['Commodity_type'].value_counts().values
fig = go.Figure()
fig.add_trace(
go.Bar(x=import_commoditytype_names,
y=import_commoditytype_values,
orientation='v',
marker_color=px.colors.sequential.Mint +
px.colors.sequential.Cividis + ['#DAA520', '#CD5C5C', '#20B2AA']))
fig.update_layout(title_text='Bar Chart of Import Commodity Types',
title_font_size=30,
xaxis_title_text='Commodities',
xaxis_title_font_size=20,
yaxis_title_text='Count',
yaxis_title_font_size=20,
template='plotly_dark')
fig.show()
fig = go.Figure()
fig.add_trace(
go.Bar(x=export_commoditytype_names,
y=export_commoditytype_values,
orientation='v',
marker_color=px.colors.sequential.Mint +
px.colors.sequential.Cividis + ['#DAA520', '#CD5C5C', '#20B2AA']))
fig.update_layout(title_text='Bar Chart of Export Commodity Types',
title_font_size=30,
xaxis_title_text='Commodities',
xaxis_title_font_size=20,
yaxis_title_text='Count',
yaxis_title_font_size=20,
template='plotly_dark')
fig.show()
import_commoditytype_names, import_commoditytype_values = import_data[
'Commodity_type'].value_counts(
).index, import_data['Commodity_type'].value_counts().values
export_commoditytype_names, export_commoditytype_values = export_data[
'Commodity_type'].value_counts(
).index, export_data['Commodity_type'].value_counts().values
# Plot Choropleth map of Import Data
export_countries = export_data.groupby('country',
as_index=False).agg({'value': 'sum'})
px.choropleth(data_frame=export_countries,
locations='country',
color='value',
locationmode='country names')
export_countries=export_data.groupby('country',
as_index=False).agg({'value':'sum'})
px.choropleth(data_frame=export_countries,
locations='country',
color='value',
locationmode='country names')