Sunday, November 17, 2024
Google search engine
HomeLanguagesDelete a CSV Column in Python

Delete a CSV Column in Python

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 :

  1. With pandas library β€” drop() or pop()
  2. 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')
  1. Import Pandas
  2. Read CSV File
  3. Use drop() function for removing or deleting rows or columns from the CSV files
  4. 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')
  1. Import Pandas
  2. Read CSV File
  3. Use pop() function for removing or deleting rows or columns from the CSV files
  4. 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

  1. Open Input CSV file as source
  2. Read Source CSV File
  3. Open Output CSV File as a result
  4. 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:

RELATED ARTICLES

Most Popular

Recent Comments

κ°•μ„œκ΅¬μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
금천ꡬ좜μž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
κ΄‘λͺ…μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
κ΄‘λͺ…μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
λΆ€μ²œμΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
κ΅¬μ›”λ™μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
κ°•μ„œκ΅¬μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
μ˜€μ‚°μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
κ΄‘λͺ…μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
μ•ˆμ–‘μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
λΆ€μ²œμΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
λ™νƒ„μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
μ„œμšΈμΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
λΆ„λ‹ΉμΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
λΆ€μ²œμΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
ν™”κ³‘λ™μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
κ°•μ„œκ΅¬μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
κ³ μ–‘μΆœμž₯μ•ˆλ§ˆ on How to store XML data into a MySQL database using Python?
ν™”μ„±μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?
μ²œν˜Έλ™μΆœμž₯λ§ˆμ‚¬μ§€ on How to store XML data into a MySQL database using Python?