pandas read_csv drop column 1

pandas read_csv drop column

# Read column names from file
cols = list(pd.read_csv("sample_data.csv", nrows =1))
# Define unused cols
unused = ['col1', 'col2']
# Use list comprehension to remove the unwanted column in **usecol**
df= pd.read_csv("sample_data.csv", usecols =[i for i in cols if i not in unused])

Here is what the above code is Doing:
1. Read the first row of the file to get the column names
2. Define the columns that you want to exclude
3. Use list comprehension to create a list of columns that you want to use
4. Pass the list of columns to **usecols**

Similar Posts