Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
minichall_abolande/Got_csv_classes.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
26 lines (24 sloc)
1.11 KB
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 csv | |
# Read category.csv file to create a dictionary mapping categories to classifications | |
categories = {} | |
with open('category.csv', 'r', newline='') as category_file: | |
reader = csv.reader(category_file) | |
next(reader) # Skip header | |
for row in reader: | |
categories[row[1]] = row[0] # Map category to classification | |
# Read the train.csv file containing file names, categories, and other information | |
data = [] | |
with open('train.csv', 'r', newline='') as csv_file: | |
reader = csv.reader(csv_file) | |
header = next(reader) | |
header.append('Classification') # Add new column header | |
data.append(header) | |
for row in reader: | |
category = row[2] # Assuming categories are in the third column | |
classification = categories.get(category, 'Unknown') # Get classification from dictionary | |
row.append(classification) # Add classification to the row | |
data.append(row) | |
# Write the updated data back to the train.csv file | |
with open('train_updated.csv', 'w', newline='') as updated_csv_file: | |
writer = csv.writer(updated_csv_file) | |
writer.writerows(data) |