How to remove a column from a CSV file in Pandas
In this tutorial, you will learn how to remove specific columns from a CSV file in Python.
Comma Separated Values (CSV) Files
CSV (Comma Separated Values) files are files that are used to store tabular data such as a database or a spreadsheet. In a CSV file, tabular data is stored in plain text indicating each file as a data record.
Pandas library is used for data analysis and manipulation. It is a very powerful and easy to use library to create, manipulate and wrangle data.
read_csv and usecols
Pandas consist of read_csv function which is used to read the required CSV file and usecols is used to get the required columns.
Pandas consist of drop function which is used in removing rows or columns from the CSV files.
import pandas as pd temp=pd.read_csv('filename.csv') temp.drop('Column_name',axis=1,inplace=True) temp.head()
drop has 2 parameters ie axis and inplace.
Axis is initialized either 0 or 1. 0 is to specify row and 1 is used to specify column. Here we have set axis as 1 so that we can delete the required column, if we wanted to delete a row then axis should be set to 0.
Inplace its initialized True, which means that – do the operation inplace and return none.
We can also remove multiple columns at once, this can be done by specifying the column names a list such as [‘Column_name1′,’Column_name2’,…,].
import pandas as pd temp=pd.read_csv('filename.csv') temp.drop(['Column_name1','Column_name2',...,],axis=1,inplace=True) temp.head()