2

In my .csv file, I have 32 rows, but I only want it to return 10 rows out of those 32. So if I want to return rows 2, 7, 12, 13, 14, 17, 27, 29, 30, 32, how would I go about doing that?

I have:

read_poem_data = pd.read_csv('poem_data.csv')
print(read_poem_data.loc[2:3])

but this is only printing lines 2 and 3 of my file, when i want it to print 10 specific lines remove all the other lines from my .csv file.


1
  • read_poem_data.loc[[(line-1) for line in [2, 7, 12, 13, 14, 17, 27, 29, 30, 32]]] (because row #1 has the index of 0, etc.) Commented Jun 13, 2017 at 22:24

1 Answer 1

1

2:3 slices the data and gives you the rows 2 and 3. You have to provide all the rows you need like this

df1 = read_poem_data.loc[[2, 7, 12, 13, 14, 17, 27, 29, 30, 32]]

To get it back in csv,

df1.to_csv('poem_data.csv', index=False)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! and how would i go about removing all the unecessary rows from my .csv file?
@emptybladder Write the file back without those rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.