Skip to content
Permalink
main
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
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)