drop first column read_csv 1

drop first column read_csv

# Read column names from file
cols = list(pd.read_csv("sample_data.csv", nrows =1))
print(cols)

# 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 != 'name'])

Here is what the above code is Doing:
1. Read the first row of the file and store it in a list called **cols**
2. Use list comprehension to remove the unwanted column in **usecol**
3. Read the file again using the **usecol** parameter

Similar Posts