Delete CSV Column in Python: Quick Guide

In Python, you can use the pandas library to manipulate CSV files and remove columns. Here is a simple example:

import pandas as pd

# 读取CSV文件
df = pd.read_csv('file.csv')

# 删除指定列
df = df.drop(['column_name'], axis=1)

# 将修改后的数据保存到新的CSV文件中
df.to_csv('new_file.csv', index=False)

In this example, the CSV file is first read using the pd.read_csv function and stored in a DataFrame object. The drop method is then used to remove the specified column, with the parameter axis=1 indicating that the column should be deleted. Finally, the modified data is saved to a new CSV file using the to_csv method.

bannerAds