SomethingILearnedToday / benjamin

Portfoli
0 stars 0 forks source link

Python - Student Enrollment Bar Chart #5

Open SomethingILearnedToday opened 9 months ago

SomethingILearnedToday commented 9 months ago

import pandas as pd import matplotlib.pyplot as plt from tabulate import tabulate

def create_bar_chart(csv_file_path, column_name):

Read the CSV file into a DataFrame

df = pd.read_csv(csv_file_path)

# Check if the specified column exists in the DataFrame
if column_name not in df.columns:
    print(f"Column '{column_name}' not found in the CSV file.")
    return

# Count the occurrences of each unique value in the specified column
value_counts = df[column_name].value_counts()

# Sort the unique values in sequential order
sorted_values = value_counts.index.sort_values()

# Create a bar chart
plt.figure(figsize=(12, 6))
value_counts[sorted_values].plot(kind='bar', color='skyblue')
plt.xlabel(column_name)
plt.ylabel('Count')
plt.title(f'Bar Chart for {column_name} Counts')
plt.xticks(rotation=45, ha='right')  # Rotate x-axis labels for better readability
plt.show()

def visualize_error_table(csv_file_path):

Read the CSV file into a DataFrame

df = pd.read_csv(csv_file_path)

# Check if the 'error' column exists in the DataFrame
if 'error' not in df.columns:
    print("Column 'error' not found in the CSV file.")
    return

# Create a cross-tabulation (crosstab) of 'error' and its frequency
error_table = pd.crosstab(index=df['error'], columns='count')

# Display the error table using tabulate
print(tabulate(error_table, headers='keys', tablefmt='pretty'))

Specify the path to your CSV file and the column name

csv_file_path = 'EN_07016140002_02232024_001(1).csv' column_name = 'Grade'

Call the function to create the bar chart with counts

create_bar_chart(csv_file_path, column_name)

Call the function to create and display the error table

visualize_error_table(csv_file_path)

SomethingILearnedToday commented 9 months ago

Skills: Analytics, Communication, Data Analytics, Data Science, and Visualization Analytical Skills, Data Analysis, Data Pipelines, Datasets, and Problem Solving