This guide demonstrates how to apply one-hot encoding to selected columns ('proto', 'service', 'state', 'attack_cat') within a CSV file using the Pandas library in Python. The process involves removing unnecessary columns ('id', 'label') and saving the encoded data to a new CSV file.

import pandas as pd

# Load the CSV file
data = pd.read_csv('input.csv')

# Specify columns for one-hot encoding
columns_to_encode = ['proto', 'service', 'state', 'attack_cat']

# Perform one-hot encoding
encoded_data = pd.get_dummies(data[columns_to_encode])

# Drop 'id' and 'label' columns
encoded_data = encoded_data.drop(['id', 'label'], axis=1)

# Save to a new CSV file
encoded_data.to_csv('output.csv', index=False)

Important Note: This code assumes the presence of 'id' and 'label' columns in your input CSV. Adjust the code accordingly if your file structure differs.

Pandas CSV One-Hot Encoding: Transform 'proto', 'service', 'state', 'attack_cat' Columns

原文地址: https://www.cveoy.top/t/topic/i5f2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录