Delete a CSV Column in Python
Last Updated :
23 May, 2024
The comma-separated values (CSV) file is a delimited text file that uses commas for individual values. Each line of the file is a data record in CSV. This format used for tabular data, rows, and columns, exactly like a spreadsheet. The CSV file stores data in rows and the values in each row are separated with a comma(separator), also known as a delimiter.
There are 2 ways to remove a column entirely from a CSV in python. Let us now focus on the techniques :
- With pandas library — drop() or pop()
- Without pandas library
Here, a simple CSV file is used i.e;input.csv
id | day | month | year | item_quantity | Name |
1 | 12 | 3 | 2020 | 12 | Oliver |
2 | 13 | 3 | 2020 | 45 | Henry |
3 | 14 | 3 | 2020 | 8 | Benjamin |
4 | 15 | 3 | 2020 | 23 | John |
5 | 16 | 3 | 2020 | 31 | Camili |
6 | 17 | 3 | 2020 | 40 | Rheana |
7 | 18 | 3 | 2020 | 55 | Joseph |
8 | 19 | 3 | 2020 | 13 | Raj |
9 | 20 | 3 | 2020 | 29 | Elias |
10 | 21 | 3 | 2020 | 19 | Emily |
Method 1: Using pandas library
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas consist of a drop function that is used in removing rows or columns from the CSV files. Pandas Pop() method is common in most of the data structures but the pop() method is a little different from the rest. In a stack, pop doesn’t require any parameters, it pops the last element every time. But the pandas pop method can take input of a column from a data frame and pop that directly.
Example 1: Using drop()
data.drop( labels=None, axis=0, index=None, columns=None, level=None, inplace=False,errors='raise')
- Import Pandas
- Read CSV File
- Use drop() function for removing or deleting rows or columns from the CSV files
- Print Data
Python3
# import pandas with shortcut 'pd'
import pandas as pd
# read_csv function which is used to read the required CSV file
data = pd.read_csv('input.csv')
# display
print("Original 'input.csv' CSV Data: \n")
print(data)
# drop function which is used in removing or deleting rows or columns from the CSV files
data.drop('year', inplace=True, axis=1)
# display
print("\nCSV Data after deleting the column 'year':\n")
print(data)
Output:
Example 2: Using pop()
We can use the panda pop () method to remove columns from CSV by naming the column as an argument.
data.pop('column-name')
- Import Pandas
- Read CSV File
- Use pop() function for removing or deleting rows or columns from the CSV files
- Print Data
Python3
# import pandas with shortcut 'pd'
import pandas as pd
# read_csv function which is used to read the required CSV file
data = pd.read_csv('input.csv')
# display
print("Original 'input.csv' CSV Data: \n")
print(data)
# pop function which is used in removing or deleting columns from the CSV files
data.pop('year')
# display
print("\nCSV Data after deleting the column 'year':\n")
print(data)
Output:
Method 2: Using CSV library
Example 3: Using CSV read and write
- Open Input CSV file as source
- Read Source CSV File
- Open Output CSV File as a result
- Put source CSV data in result CSV using indexes
Python3
# import csv
import csv
# open input CSV file as source
# open output CSV file as result
with open("input.csv", "r") as source:
reader = csv.reader(source)
with open("output.csv", "w") as result:
writer = csv.writer(result)
for r in reader:
# Use CSV Index to remove a column from CSV
#r[3] = r['year']
writer.writerow((r[0], r[1], r[2], r[4], r[5]))
Output:

Similar Reads
Add a Column to Existing CSV File in Python Working with CSV files is a common task in data manipulation and analysis, and Python provides versatile tools to streamline this process. Here, we have an existing CSV file and our task is to add a new column to the existing CSV file in Python. In this article, we will see how we can add a column t
3 min read
How to delete a CSV file in Python? In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
2 min read
Convert Excel to CSV in Python In this article, we will be dealing with the conversion of Excel (.xlsx) file into .csv. Â There are two formats mostly used in Excel : (*.xlsx) : Excel Microsoft Office Open XML Format Spreadsheet file.(*.xls) : Excel Spreadsheet (Excel 97-2003 workbook). Let's Consider a dataset of a shopping store
3 min read
Python - Read CSV Columns Into List CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. In this article, we will read data from a
3 min read
Replacing column value of a CSV file in Python Let us see how we can replace the column value of a CSV file in Python. CSV file is nothing but a comma-delimited file. Method 1: Using Native Python way Using replace() method, we can replace easily a text into another text.  In the below code, let us have an input CSV file as "csvfile.csv" and be
2 min read
Get column names from CSV using Python CSV (Comma Separated Values) files store tabular data as plain text, with values separated by commas. They are widely used in data analysis, machine learning and statistical modeling. In Python, you can work with CSV files using built-in libraries like csv or higher-level libraries like pandas. In t
2 min read
How to Sort data by Column in a CSV File in Python ? In this article, we will discuss how to sort CSV by column(s) using Python. Method 1: Using sort_values() We can take the header name as per our requirement, the axis can be either 0 or 1, where 0 means 'rows' and '1' means 'column'. Ascending can be either True/False and if True, it gets arranged i
3 min read
How to delete columns in PySpark dataframe ? In this article, we are going to delete columns in Pyspark dataframe. To do this we will be using the drop() function. This function can be used to remove values from the dataframe. Syntax: dataframe.drop('column name') Python code to create student dataframe with three columns: Python3 # importing
2 min read
Python - Read CSV Column into List without header Prerequisites: Reading and Writing data in CSVÂ CSV files are parsed in python with the help of csv library. Â The csv library contains objects that are used to read, write and process data from and to CSV files. Sometimes, while working with large amounts of data, we want to omit a few rows or colum
2 min read
Drop Empty Columns in Pandas Cleaning data is an essential step in data analysis. In this guide we will explore different ways to drop empty, null and zero-value columns in a Pandas DataFrame using Python. By the end you'll know how to efficiently clean your dataset using the dropna() and replace() methods. Understanding dropna
3 min read