Reputation: 13
I have put numerous CSV files in a fold and would like to skip the certain row (e.g. the 10th row) first, and then take one row every five lines.
I could do the first step however have no idea about the second one.
Thanks.
import pandas as pd
import csv, os
# Loop through every file in the current working directory.
for csvFilename in os.listdir('path'):
if not csvFilename.endswith('.csv'):
continue
# Now let's read the dataframe
# total row number
total_line = len(open('path' + csvFilename).readlines())
# put the first and last to a list
line_list = [total_line] + [1]
df = pd.read_csv('path' + csvFilename, skiprows=line_list)
new_file_name = csvFilename
# And output
df.to_csv('path' + new_file_name, index=False)
The correct code is shown as follows.
import numpy as np
import pandas as pd
import csv, os
# Loop through every file in the current working directory.
for csvFilename in os.listdir('path'):
if not csvFilename.endswith('.csv'):
continue
# Now let's read the dataframe
total_line = len(open('path' + csvFilename).readlines())
skip = np.arange(total_line)
# skip 5 rows
skip = np.delete(skip, np.arange(0, total_line, 5))
# skip the certain row you would like, e.g. 10
skip = np.append(skip, 10)
df = pd.read_csv('path' + csvFilename, skiprows=skip)
new_file_name = '2' + csvFilename
# And output
df.to_csv('path' + new_file_name, index=False)
Upvotes: 0
Views: 391
Reputation: 464
You can use a function with skiprows
.
I edited your code below:
import numpy as np
import csv, os
# Loop through every file in the current working directory.
for csvFilename in os.listdir('path'):
if not csvFilename.endswith('.csv'):
continue
# Now let's read the dataframe
total_line = len(open('path' + csvFilename).readlines())
df = pd.read_csv('path' + csvFilename, skiprows=lambda x: x in list(range(total_line))[1:-1:5])
new_file_name = csvFilename
# And output
df.to_csv('path' + new_file_name, index=False)
Upvotes: 1