Skip to content
Advertisement

How do I slice a numpy array to get both the first and last two rows

As far as I can see it, it isn’t covered in the indexing, slicing and iterating scipy tutorial, so let me ask it here:

Say I’ve

JavaScript

How do I slice the array in order get both the first and last rows:

JavaScript

Advertisement

Answer

I don’t know if there’s a slick way to do that. You could list the indices explicitly, of course:

JavaScript

Or use r_ to help, which would probably be more convenient if we wanted more rows from the head or tail:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement