I have a CSV file.
How do I read specific rows?
I want to read the rows in the even positions?
import csv
with open('source.csv','rt')as f:
data = csv.reader(f)
for row in data:
print(row)
I have a CSV file.
How do I read specific rows?
I want to read the rows in the even positions?
import csv
with open('source.csv','rt')as f:
data = csv.reader(f)
for row in data:
print(row)
import pandas as pd
dataframe = pd.read_csv('your_file')
dataframe_even_rows = dataframe.iloc[::2]
here you go:
import csv
even_rows = []
# reading the csv
with open("file.csv", "r+") as csv_file:
# reading all lines
lines = csv.reader(csv_file, delimiter=",")
# extracting only even lines
for i, line in zip(range(lines.line_num), lines):
if i % 2 == 0:
even_rows.append(line)
# printing output
for row in even_rows:
print(row)
Extending the solution provided by Adrien.
import pandas as pd
n = 1 #Number of rows you want to skip
dataframe = pd.read_csv('your_file')
dataframe_even_rows = dataframe.iloc[::(n+1)]
dataframe_specific_row = dataframe.iloc[2] #Specific lines: read third line(index start at 0)