How To Delete Rows (not Columns) In A Csv File
I am trying to delete a particular row (NOT a column) in a csv file for a class project. When I deleted columns I put: r=row r[22], r[21] # and so on So how do I specify that
Solution 1:
Convert your csv reader to a list and slice the appropriate indexes off:
import csv
withopen('file.csv', 'rb') as f:
reader = csv.reader(f)
rows = list(reader)[1:] # no more header row
Solution 2:
Use pandas, it's so easy to handle data and files with it. Use it to edit your data easily.
You can open your csv file and convert it to a pandas dataframe through.
df = pandas.read_csv('file.csv')
After that you can use this function.
df.drop(df.columns[[0]], axis=1)
In this example I'm deleting the row with index 0.
Post a Comment for "How To Delete Rows (not Columns) In A Csv File"