0

I want to work with the data imported from csv files. However, there are many lines of information that I don´t need in the csv files. Let´s say, data from the first three rows and all rows after 125 should be removed. How can I get this job done by using Python? I have figured out the way to remove the first three rows but I am still having problem with the rest part.

import  csv

csv_file = open('Raman_060320.csv')
csv_reader = csv.reader(csv_file, delimiter='\t')

for skip in range(3):
    next(csv_reader)

for row in csv_reader:
    print(row)

csv_file.close()

I am from the field of hydrology and don´t know very deep about programming (I´ve just began to learn), so I would appreciate all the help I could get.

1 Answers1

0

As suggested by Damzaky, using pandas:

import pandas as pd

df = pd.read_csv('Raman_060320.csv')
#Keep rows 4 - 125
df = df[3:126]
#Save to csv
df.to_csv('Raman_060320.csv', index = False)
Victor Wang
  • 765
  • 12
  • 26
  • Thank you for your answer! It helped me a lot. However, I also have to (and would like to) define the types of the data I need. I just know the #read_csv(dtype)#, however the wanted (4th to 125th) rows are already imported as dataframe. Is there any solution? – Michael Gao Mar 21 '20 at 19:42
  • You can read all columns as string and convert them into the data types you want. See here: https://stackoverflow.com/questions/49684951/pandas-read-csv-dtype-read-all-columns-but-few-as-string – Victor Wang Mar 21 '20 at 20:10