custom-scripts/mccs-it/UEFilterer.py
2024-08-13 09:48:52 -04:00

30 lines
1.1 KiB
Python

# Filters Brightspace's DataHub's UserEnrollments.csv file for a single college
# Install Python on Windows from Powershell by running `python` with no arguments
# pip install pandas
import pandas as pd
# Variables
college = 'KV'
ue_input_file = '.\\data\\UserEnrollments.csv'
ue_output_file = '.\\data\\UEFiltered' + college + '.csv'
ue_field_name = 'RoleName'
ue_filter_value = college + ' Instructor'
users_input_file = '.\\data\\Users.csv'
users_output_file = '.\\data\\UsersFiltered' + college + '.csv'
users_field_name = 'UserName'
users_filter_value = "KV_"
def filter_csv(input_file, output_file, field_name, filter_value):
# Read the CSV file
df = pd.read_csv(input_file)
# Filter the DataFrame based on the field value
filtered_df = df[df[field_name].str.contains(filter_value, na=False)]
# Export the filtered DataFrame to a new CSV file
filtered_df.to_csv(output_file, index=False)
filter_csv(ue_input_file, ue_output_file, ue_field_name, ue_filter_value)
filter_csv(users_input_file, users_output_file, users_field_name, users_filter_value)