0

I have about 20 rows of data with 4 columns each. How do I print only a certain row. Like for instance print only Row 15, Row 16 and Row 17. When I try row[0] it only prints out the first column but not the entire row. I am confused here.

Right now I can read out each of the rows by doing:

for lines in reader: print(lines)

2 Answers2

0

Try iloc method

import pandas as pd
## suppose you want 15 row only
data=pd.read_csv("data.csv").iloc[14,:]
simar
  • 605
  • 4
  • 12
0

If you have relatively small dataset you can read the entire thing and select the rows you want

reader = csv.reader(open("somefile.csv"))
table = list(reader) # reads entire file
for row in table[15:18]:
    print(row)

You could also save a bit of time by only reading as much as you need

with open("somefile.csv") as f:
    reader = csv.reader(f)
    for _ in range(14):
        next(reader)  # dicarc
    for _ in range(3):
        print(next(reader))
tdelaney
  • 73,364
  • 6
  • 83
  • 116