Assignment 12
Assignment 12
import csv
rows = [
filename = 'data.csv'
writer = csv.writer(file)
import pandas as pd
# Load the CSV file into a DataFrame
df = pd.read_csv('data.csv')
print("Original DataFrame:")
print(df)
print(df_encoded)
df_encoded.to_csv('data_encoded.csv', index=False)
output:-
Original DataFrame:
0 Alice 0 0 ... 0 0 1
1 Bob 1 0 ... 0 1
0
2 Charlie 0 0 ... 1 0
0
3 David 0 1 ... 0 1
0
4 Eva 1 0 ... 0 0
1
[5 rows x 7 columns]
1. Load the CSV File: Use Pandas to read the data from a CSV file
into a DataFrame.
3. Display or Save the Result: Show the DataFrame with the one-hot
encoded features, and optionally save it to a new CSV file.
Explanation
2. One-Hot Encoding: