text_html_output_1.png
import numpy as np

import pandas as pd

import plotly.graph_objects as go

import pycountry

import warnings

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings(action='ignore')
import plotly as py
import plotly.graph_objects as go
from plotly import tools
def drop(df):
    df = df.drop(df.index[0])
    return df
df_2017 = pd.read_csv('../input/kaggle-survey-2017/multipleChoiceResponses.csv', encoding='latin1')
df_2018 = pd.read_csv('../input/kaggle-survey-2018/multipleChoiceResponses.csv', encoding='latin1')
df_2019 = pd.read_csv('../input/kaggle-survey-2019/multiple_choice_responses.csv')
df_2020 = pd.read_csv('../input/kaggle-survey-2020/kaggle_survey_2020_responses.csv')
df_2018 = drop(df_2018)
df_2019 = drop(df_2019)
df_2020 = drop(df_2020)
num_qn = df_2020.columns
stat_2017 = df_2017['GenderSelect'][0:].value_counts()
f_2017 = round(stat_2017['Female'] / np.sum(stat_2017) * 100, 2)
stat_2018 = df_2018['Q1'][0:].value_counts()
f_2018 = round(stat_2018['Female'] / np.sum(stat_2018) * 100, 2)
stat_2019 = df_2019['Q2'][0:].value_counts()
f_2019 = round(stat_2019['Female'] / np.sum(stat_2019) * 100, 2)
stat_2020 = df_2020['Q2'][0:].value_counts()
f_2020 = round(stat_2020['Woman'] / np.sum(stat_2020) * 100, 2)

color = ['rgb(49,130,189)']
color_m = ['rgb(49,130,189)', '#de6560']
mode_size = [12]
line_size = [5]
x_data = np.vstack((np.arange(2017, 2021),) * 1)
y_data = np.array([[f_2017, f_2018, f_2019, f_2020]])
fig = go.Figure()
for i in range(0, 1):
    fig.add_trace(go.Scatter(x=x_data[i], y=y_data[i], mode='lines', line=dict(color=color[i], width=line_size[i]), connectgaps=True))
    fig.add_trace(go.Scatter(x=[x_data[i][0], x_data[i][-1]], y=[y_data[i][0], y_data[i][-1]], mode='markers', marker=dict(color=color_m[i], size=mode_size[i])))
fig.update_layout(title="<span style='color:#000; font-size:25px; font-family:PT Sans'>Relative number of female participants</span>", xaxis=dict(showline=True, showgrid=False, showticklabels=True, linecolor='rgb(204, 204, 204)', linewidth=2, ticks='outside', tickfont=dict(family='PT sans', size=15, color='rgb(82, 82, 82)')), yaxis=dict(showgrid=False, zeroline=False, showline=False, showticklabels=False), autosize=False, margin=dict(autoexpand=True, l=200, r=20, t=100), width=600, height=400, showlegend=False, plot_bgcolor='white')
annotations = []
for y_trace, color in zip(y_data, color):
    annotations.append(dict(xref='paper', x=0.04, y=y_trace[0], xanchor='left', yanchor='bottom', text='{}%'.format(y_trace[0]), font=dict(family='PT sans', size=18, color='rgb(82, 82, 82)'), showarrow=False))
    annotations.append(dict(xref='paper', x=0.9, y=y_trace[3], xanchor='right', yanchor='middle', text='{}%'.format(y_trace[3]), font=dict(family='PT sans', size=18, color='rgb(82, 82, 82)'), showarrow=False))
annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.2, xanchor='center', yanchor='top', text='Source: 2017 - 2020 Kaggle Machine Learning & ' + 'Data Science Survey', font=dict(family='PT sans', size=12, color='rgb(150,150,150)'), showarrow=False))
fig.update_layout(annotations=annotations)

age_2020 = df_2020[df_2020['Q2'] == 'Woman']['Q1'].value_counts()
age = []
percent_age = []
for i, j in enumerate(age_2020.index):
    age.append(j)
    percent_age.append(round(age_2020[i] / np.sum(age_2020) * 100, 2))
color_first = '#de6560'
color_rest = '#98c1d9'
x_data = age
y_data = percent_age
white = "<b style='color: #fff; font-size:15px; font-family:PT Sans'> %{text}% </b>"
black = "<b style='color: #000; font-size:15px; font-family:PT Sans'> %{text}% </b>"
trace = go.Bar(y=x_data, x=y_data, orientation='h', marker=dict(color=[color_first] + [color_rest] * 10), text=y_data, texttemplate=[white] * 6 + [black] * 5, textposition=['inside'] * 6 + ['outside'] * 5)
layout = dict(title="<span style='color:#000; font-size:25px; font-family:PT Sans'>Age groups</span><br><span style='color:#969696; font-size: 20px; font-family:PT Sans'>female paricipants</span><br>", margin=dict(t=150), legend=dict(orientation='h', yanchor='top', xanchor='center', y=1.06, x=0.5, font=dict(size=16)), xaxis=dict(side='top', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(204, 204, 204)', linewidth=2, ticks='outside', tickfont=dict(family='PT sans', size=14, color='rgb(82, 82, 82)')), yaxis=dict(autorange='reversed', tickfont=dict(family='PT sans', size=18), color='rgb(82, 82, 82)'), barmode='group', bargap=0.05, bargroupgap=0.1, width=800, height=600, plot_bgcolor='white')
fig = go.Figure(data=trace, layout=layout)
fig.add_annotation(dict(xref='paper', yref='paper', x=0.5, y=0, xanchor='center', yanchor='top', text='Source: 2020 Kaggle Machine Learning & ' + 'Data Science Survey', font=dict(family='PT sans', size=12, color='rgb(150,150,150)'), showarrow=False))

import pycountry
country_2020 = df_2020[df_2020['Q2'] == 'Woman']['Q3']
country_2020 = country_2020.value_counts()
country = []
percent = []
for i, j in enumerate(country_2020.index):
    country.append(j)
    percent.append(round(country_2020[i] / np.sum(country_2020) * 100, 3))
country[1] = 'United States'
country[4] = 'United Kingdom'
country[7] = 'Russian Federation'
country[12] = 'Iran, Islamic Republic of'
country[13] = 'Taiwan, Province of China'
country[20] = 'Korea, Republic of'
country[-1] = "Korea, Democratic People's Republic of"
input_countries = country
countries = {}
for cntry in pycountry.countries:
    countries[cntry.name] = cntry.alpha_3
codes = [countries.get(cntry, 'Unknown code') for cntry in input_countries]
del codes[2:3]
del percent[2:3]

fig = go.Figure(data=go.Choropleth(locations=codes, z=percent, text=percent, colorscale='Reds', autocolorscale=False, reversescale=False, marker_line_color='darkgray', marker_line_width=0.5, colorbar_title="<span style='color:#000; font-size:16px; font-family:PT Sans'>Percentage</span><br>"))
fig.update_layout(title="<span style='color:#000; font-size:25px; font-family:PT Sans'>Location</span><br><span style='color:#969696; font-size: 20px; font-family:PT Sans'>female participants</span><br>", margin=dict(t=150), geo=dict(showframe=False, showcoastlines=False, projection_type='equirectangular'), width=700, height=600, annotations=[dict(xref='paper', yref='paper', x=0.5, y=0, xanchor='center', yanchor='top', text='Source: 2020 Kaggle Machine Learning & ' + 'Data Science Survey', font=dict(family='PT sans', size=12, color='rgb(150,150,150)'), showarrow=False)])

education_2020 = df_2020[df_2020['Q2'] == 'Woman']['Q4'].value_counts()
education = []
percent_edu = []
for i, j in enumerate(education_2020.index):
    education.append(j)
    percent_edu.append(round(education_2020[i] / np.sum(education_2020) * 100, 2))

color_first = '#de6560'
color_rest = '#98c1d9'
x_data = percent_edu
y_data = education
white = "<b style='color: #fff; font-size:15px; font-family:PT Sans'> %{text}% </b>"
black = "<b style='color: #000; font-size:15px; font-family:PT Sans'> %{text}% </b>"
trace = go.Bar(x=x_data, y=y_data, orientation='h', marker=dict(color=[color_first] + [color_rest] * 6), text=x_data, texttemplate=[white] * 2 + [black] * 5, textposition=['inside'] * 2 + ['outside'] * 5)
layout = dict(title="<span style='color:#000; font-size:25px; font-family:PT Sans'>Level of education</span><br><span style='color:#969696; font-size: 20px; font-family:PT Sans'>female participants</span><br>", margin=dict(t=150), legend=dict(orientation='h', yanchor='top', xanchor='center', y=1.06, x=0.5, font=dict(size=16)), xaxis=dict(autorange='reversed', side='right', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(204, 204, 204)', linewidth=2, ticks='outside', tickfont=dict(family='PT sans', size=14, color='rgb(82, 82, 82)')), yaxis=dict(autorange='reversed', side='right', tickfont=dict(family='PT sans', size=18, color='rgb(82, 82, 82)')), barmode='group', bargap=0.05, bargroupgap=0.1, width=700, height=600, plot_bgcolor='white')
fig = go.Figure(data=trace, layout=layout)
fig.add_annotation(dict(xref='paper', yref='paper', x=1, y=-0.11, xanchor='center', yanchor='top', text='Source: 2020 Kaggle Machine Learning & ' + 'Data Science Survey', font=dict(family='PT sans', size=12, color='rgb(150,150,150)'), showarrow=False))

qns = [x for x in num_qn]
def data(qnn):
    qn = [x for x in qns if qnn in x]
    name = []
    for q in qn:
        for x in df_2020[q].unique():
            name.append(x)
    name = [x for x in name if str(x) != 'nan']
    name = [x.strip(' ') for x in name]
    name_percent = (df_2020.shape[0] - df_2020[qn].isnull().sum()) / df_2020.shape[0]
    name_percent.index = name
    name_percent = name_percent.sort_values(ascending=False)
    name = []
    percent_name = []
    for i, j in enumerate(name_percent.index):
        name.append(j)
        percent_name.append(round(name_percent[i] * 100, 2))
    return (name, percent_name)

language, percent_lng = data('Q7')
color_first = '#de6560'
color_rest = '#98c1d9'
x_data = percent_lng
y_data = language
white = "<b style='color: #fff; font-size:15px; font-family:PT Sans'> %{text}% </b>"
black = "<b style='color: #000; font-size:15px; font-family:PT Sans'> %{text}% </b>"
trace = go.Bar(x=x_data, y=y_data, orientation='h', marker=dict(color=[color_first] + [color_rest] * 12), text=x_data, texttemplate=[white] * 7 + [black] * 6, textposition=['inside'] * 7 + ['outside'] * 6)
layout = dict(title="<span style='color:#000; font-size:25px; font-family:PT Sans'>Programming language</span><br><span style='color:#969696; font-size: 20px; font-family:PT Sans'>all participants</span><br>", margin=dict(t=150), legend=dict(orientation='h', yanchor='top', xanchor='center', y=1.06, x=0.5, font=dict(size=16)), xaxis=dict(side='top', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(204, 204, 204)', linewidth=2, ticks='outside', tickfont=dict(family='PT sans', size=14, color='rgb(82, 82, 82)')), yaxis=dict(autorange='reversed', tickfont=dict(family='PT sans', size=18, color='rgb(82, 82, 82)')), barmode='group', bargap=0.05, bargroupgap=0.1, width=700, height=600, plot_bgcolor='white')
fig = go.Figure(data=trace, layout=layout)
fig.add_annotation(dict(xref='paper', yref='paper', x=0.5, y=0, xanchor='center', yanchor='top', text='Source: 2020 Kaggle Machine Learning & ' + 'Data Science Survey', font=dict(family='PT sans', size=12, color='rgb(150,150,150)'), showarrow=False))
fig.show()