0

I'm a bit stuck trying to understand this line of code.

women_only_stats = data[0::,4] == "female" 

I'm confused with the part of data. What exactly does the double colon mean with the comma 4 next to it?

3
  • 2
    [start:stop:step], where the comma comes from? Commented Nov 21, 2015 at 13:03
  • i know the start and stop procedure its just i dont know the difference between [::] and using [:] Commented Nov 21, 2015 at 13:13
  • As far as I know, that's a syntax error. The comma should not be there. Commented Nov 21, 2015 at 13:32

1 Answer 1

1

This isn't a syntax error, and probably isn't even a mistake. We'd need to see data to be sure. Numpy arrays, among others, accept a tuple of slices as an index, so it's certainly not impossible.

See here for an introduction to numpy indexing.

The 0:: part is basically the same as : or ::, at least if we're using the standard slice interpretation. (Remember that slice objects can be made of many types, and so things like "A":10:(1,2) are perfectly valid syntactically-- it's the object's responsibility to decide how it wants to handle that input.)

I suspect that data is a numpy array looking something like

>>> data
array([[1, 2, 5, 12, 'male'],
       [1, 2, 5, 12, 'female'],
       [1, 2, 5, 12, 'female']], dtype=object)
>>> data[:,4]
array(['male', 'female', 'female'], dtype=object)

:,4, or ::,4, or 0::,4, all mean "give me every value along axis 0 (the rows), and column #4". We can then do a vectorized comparison:

>>> data[:,4] == "female"
array([False,  True,  True], dtype=bool)

Finally, we can use that to filter data:

>>> female_only_stats = data[:,4] == "female"
>>> data[female_only_stats]
array([[1, 2, 5, 12, 'female'],
       [1, 2, 5, 12, 'female']], dtype=object)
Sign up to request clarification or add additional context in comments.

Comments

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.