Delete a CSV Column in Python - GeeksforGeeks

02 Feb, 2021
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
With pandas library — drop() or pop()
Without pandas library
Here, a simple CSV file is used i.e; input.csv
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 PandasRead CSV FileUse drop() function for removing or deleting rows or columns from the CSV filesPrint Data
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 filedata = 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 filesdata.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.
Import PandasRead CSV FileUse pop() function for removing or deleting rows or columns from the CSV filesPrint Data
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 filedata = pd.read_csv('input.csv')  # displayprint("Original 'input.csv' CSV Data: \n")print(data)  # pop function which is used in removing or deleting columns from the CSV filesdata.pop('year')  # displayprint("\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 sourceRead Source CSV FileOpen Output CSV File as a resultPut source CSV data in result CSV using indexes
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 csvimport csv  # open input CSV file as source# open output CSV file as resultwith 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:
Picked
python-csv
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python