Last active
October 15, 2022 22:21
-
-
Save gustheman/c26cc3966820190f64c1d45168d4d0e6 to your computer and use it in GitHub Desktop.
This is a simple function to plot results from the Kaggle Kernel Survey (2021 and 2022). It will work only on multiple choise questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import plotly.express as px | |
df = pd.read_csv("kaggle_survey_2022_responses.csv") | |
def plot_answers(data, question_prefix, year): | |
filter_columns = [col for col in data.columns if col.startswith(question_prefix)] | |
df = data[filter_columns] | |
question = df.iloc[0][0].split("?")[0] | |
df = df.drop(index=0, axis=0) | |
df.dropna(how='all', inplace=True) | |
t = df.stack().reset_index()[0] | |
series = t.value_counts().sort_values() | |
num_questions = len(filter_columns) | |
num_rows = len(df) | |
labels = [f"{v} ({(v/num_rows)*100:.2f}%)" for v in series] | |
fig = px.bar(series, | |
labels={"": '',"value": ""}, | |
text=labels, | |
orientation="h",) | |
fig.update_layout(showlegend=False, | |
title={'text': f'{question} in {year}', | |
'y':0.95, | |
'x':0.5,}, | |
xaxis_range=[0,series[-1]*1.1]) | |
fig.write_image(f'{question_prefix}_{year}.png') | |
fig.show() | |
# example usage for used programming languages | |
plot_answers(df, "Q12", 2022) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment