0% found this document useful (0 votes)
7 views9 pages

Stack Overflow Data Analysis Guide

The document outlines a series of steps for data analysis on a Stack Overflow dataset using Python's pandas and numpy libraries. It includes loading the data, cleaning it by handling missing values, filtering specific groups, and performing various analyses such as calculating average ages, employment statistics, and compensation insights. The final results are saved into cleaned CSV files for further use.

Uploaded by

ivanolumosi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Stack Overflow Data Analysis Guide

The document outlines a series of steps for data analysis on a Stack Overflow dataset using Python's pandas and numpy libraries. It includes loading the data, cleaning it by handling missing values, filtering specific groups, and performing various analyses such as calculating average ages, employment statistics, and compensation insights. The final results are saved into cleaned CSV files for further use.

Uploaded by

ivanolumosi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# 📌 Step 1: Import necessary libraries

import pandas as pd

import numpy as np

# 📌 Step 2: Load the dataset (Replace with your actual path or use an online URL if needed)

# Assuming your dataset is named '[Link]'

df = pd.read_csv("[Link]", index_col="Respondent") # 'Respondent' as unique identifier

# 📌 Step 3: Preview the data

print([Link](5)) # See the first 5 rows

print([Link]) # Show all column names

print([Link]()) # Get data types and non-null values

print([Link](include='all')) # Summary statistics (include non-numeric)

# 📌 Step 4: Check missing values

print([Link]().sum().sort_values(ascending=False)) # See which columns have most missing data

# 📌 Step 5: Drop columns with >80% missing values (optional, depends on your needs)

threshold = len(df) * 0.8

df = [Link][:, [Link]().sum() < threshold] # Keep only columns with <80% missing values

# 📌 Step 6: Fill missing values in specific columns

df['Student'] = df['Student'].fillna('Not a student') # Replace NaN in Student column

df['Age'] = df['Age'].fillna(df['Age'].median()) # Replace missing Age with median

# 📌 Step 7: Filter: Find all students who are unemployed

students_unemployed = df[(df['Student'].[Link]('Yes', na=False)) &

(df['Employment'] == 'Not employed, but looking for work')]

print(students_unemployed[['Country', 'Age', 'EdLevel']])


# 📌 Step 8: Group by country and calculate average age

avg_age_per_country = [Link]('Country')['Age'].mean().sort_values(ascending=False)

print(avg_age_per_country)

# 📌 Step 9: Count how many people work with open source regularly

open_sourcers = df['OpenSourcer'].value_counts()

print(open_sourcers)

# 📌 Step 10: Use NumPy to analyze Age

ages = df['Age'].dropna().to_numpy()

print("Average Age:", [Link](ages))

print("Standard Deviation:", [Link](ages))

print("Minimum Age:", [Link](ages))

print("Maximum Age:", [Link](ages))

# 📌 Step 11: Crosstab - Gender vs Employment

gender_vs_employment = [Link](df['Gender'], df['Employment'], normalize='index')

print(gender_vs_employment)

# 📌 Step 12: Apply function to classify age groups

def age_group(age):

if age < 18:

return 'Teen'

elif age < 30:

return 'Young Adult'

elif age < 50:

return 'Adult'

else:
return 'Senior'

df['AgeGroup'] = df['Age'].apply(age_group)

print(df[['Age', 'AgeGroup']].head())

# 📌 Step 13: Group by AgeGroup and count

agegroup_count = df['AgeGroup'].value_counts()

print(agegroup_count)

# 📌 Step 14: Which language was most commonly used?

# Many responses have multiple languages separated by semicolons, so we’ll split them and count

language_series = df['LanguageWorkedWith'].dropna().[Link](';')

all_languages = [Link]([[Link]() for sublist in language_series for lang in sublist])

print(all_languages.value_counts().head(10)) # Top 10 most popular languages

# 📌 Step 15: Save cleaned data for later use

df.to_csv("cleaned_stackoverflow.csv")

# 📌 Done!

# 📌 Step 1: Import the libraries

import pandas as pd

import numpy as np

# 📌 Step 2: Load the dataset


df = pd.read_csv("[Link]", index_col="Respondent")

# 📌 Step 3: Clean and fill key identity columns

df['Gender'] = df['Gender'].fillna('Not disclosed')

df['Trans'] = df['Trans'].fillna('Not disclosed')

df['Sexuality'] = df['Sexuality'].fillna('Not disclosed')

df['Ethnicity'] = df['Ethnicity'].fillna('Not disclosed')

df['Age'] = df['Age'].fillna(df['Age'].median()) # Replace NaNs in Age with median age

# 📌 Step 4: View value counts

print("Gender Distribution:\n", df['Gender'].value_counts())

print("\nTrans Identity Distribution:\n", df['Trans'].value_counts())

print("\nSexuality Distribution:\n", df['Sexuality'].value_counts())

print("\nEthnicity Distribution:\n", df['Ethnicity'].value_counts().head(10)) # Top 10 ethnicities

# 📌 Step 5: Basic age statistics

print("\nAge Statistics:")

print("Mean:", df['Age'].mean())

print("Median:", df['Age'].median())

print("Min:", df['Age'].min())

print("Max:", df['Age'].max())

# 📌 Step 6: Filter people under 20 who identify as non-binary or gender diverse

young_nonbinary = df[(df['Age'] < 20) &

(~df['Gender'].[Link]('Man|Woman', na=False))]

print("\nYoung non-binary respondents:\n", young_nonbinary[['Country', 'Gender', 'Age',


'Sexuality']].head())

# 📌 Step 7: Crosstab Gender vs. Employment


gender_employment = [Link](df['Gender'], df['Employment'], normalize='index')

print("\nGender vs Employment:\n", gender_employment)

# 📌 Step 8: Crosstab Gender vs Career Satisfaction

gender_satisfaction = [Link](df['Gender'], df['CareerSat'], normalize='index')

print("\nGender vs Career Satisfaction:\n", gender_satisfaction)

# 📌 Step 9: Most common combinations of gender & sexuality

df['Gender_Sexuality'] = df['Gender'] + " | " + df['Sexuality']

print("\nTop Gender-Sexuality Combinations:\n", df['Gender_Sexuality'].value_counts().head(10))

# 📌 Step 10: Create age groups using NumPy

df['AgeGroup'] = [Link](df['Age'], bins=[0, 18, 25, 35, 50, 100],

labels=['<18', '18-25', '26-35', '36-50', '50+'])

print("\nAge group distribution:\n", df['AgeGroup'].value_counts())

# 📌 Step 11: Compare diversity across age groups

age_ethnicity = [Link](df['AgeGroup'], df['Ethnicity'], normalize='index')

print("\nEthnic distribution by AgeGroup:\n", age_ethnicity.head(5)) # First 5 age groups

# 📌 Step 12: Identify respondents who:

# - Are LGBTQ+ (not straight)

# - Have dependents

# - Are under 30

lgbtq_dependents_young = df[

(~df['Sexuality'].[Link]('Heterosexual|Straight', na=False)) &

(df['Dependents'] == 'Yes') &

(df['Age'] < 30)

]
print("\nLGBTQ+ respondents under 30 with dependents:\n",

lgbtq_dependents_young[['Country', 'Age', 'Sexuality', 'Dependents']].head())

# 📌 Step 13: Save results to CSV

df.to_csv("diversity_analysis_stackoverflow.csv")

# 📌 Step 1: Import libraries

import pandas as pd

import numpy as np

# 📌 Step 2: Load data

df = pd.read_csv("[Link]", index_col="Respondent")

# 📌 Step 3: Basic cleanup for relevant columns

# We'll work with: Compensation columns, Country, Employment, CareerSat, JobSat

df['CompTotal'] = pd.to_numeric(df['CompTotal'], errors='coerce')

df['CompFreq'] = df['CompFreq'].fillna('Yearly')

df['ConvertedComp'] = pd.to_numeric(df['ConvertedComp'], errors='coerce') # in USD

df['CareerSat'] = df['CareerSat'].fillna('No response')

df['JobSat'] = df['JobSat'].fillna('No response')

# 📌 Step 4: View compensation statistics


print("General compensation stats (USD converted):")

print(df['ConvertedComp'].describe())

# 📌 Step 5: Drop extremely high salaries (outliers > $300,000)

df_filtered = df[df['ConvertedComp'] < 300000]

# 📌 Step 6: Average compensation by country (Top 10)

avg_salary_by_country = df_filtered.groupby('Country')
['ConvertedComp'].mean().sort_values(ascending=False).head(10)

print("\nTop 10 countries by average compensation:")

print(avg_salary_by_country)

# 📌 Step 7: Median salary by job satisfaction level

salary_by_jobsat = df_filtered.groupby('JobSat')
['ConvertedComp'].median().sort_values(ascending=False)

print("\nMedian salary by job satisfaction level:")

print(salary_by_jobsat)

# 📌 Step 8: Count people earning over $100K by career satisfaction

rich_satisfied = df_filtered[df_filtered['ConvertedComp'] > 100000]

counts = rich_satisfied['CareerSat'].value_counts(normalize=True)

print("\nSatisfaction levels among those earning > $100K:")

print(counts)

# 📌 Step 9: Create income brackets

def income_bracket(salary):

if [Link](salary):

return 'Unknown'

elif salary < 20000:


return '<20K'

elif salary < 50000:

return '20K–50K'

elif salary < 100000:

return '50K–100K'

elif salary < 150000:

return '100K–150K'

else:

return '150K+'

df_filtered['IncomeBracket'] = df_filtered['ConvertedComp'].apply(income_bracket)

print("\nIncome bracket distribution:")

print(df_filtered['IncomeBracket'].value_counts())

# 📌 Step 10: Satisfaction by income bracket (pivot)

satisfaction_income = [Link](df_filtered['IncomeBracket'], df_filtered['JobSat'], normalize='index')

print("\nJob satisfaction by income bracket:")

print(satisfaction_income)

# 📌 Step 11: Compare compensation frequency

comp_freq_counts = df['CompFreq'].value_counts()

print("\nCompensation frequency distribution:")

print(comp_freq_counts)

# 📌 Step 12: Median salary per compensation frequency

median_per_freq = [Link]('CompFreq')['ConvertedComp'].median()

print("\nMedian compensation by frequency:")

print(median_per_freq)
# 📌 Step 13: Save cleaned results

df_filtered.to_csv("compensation_analysis_stackoverflow.csv")

You might also like