PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 15, 2022

[FIXED] Why does the index numbering of a series change when you write the name of the series twice (bracketing it the second time)?

 December 15, 2022     data-science, pandas, python, syntax     No comments   

Issue

I enter this:

test2 = [1, 2, 3, 4]
test3 = pd.Series(test2)

print(test3)
print(test3[3])
print(test3[test3[2]])

And get this:

0  1
1  2
2  3
3  4
type: int64
4
4

Basically I'm trying to understand what's happening to the index numbering. Why does a row effectively get cut, or is the index-search-result moved to the next lower row, when you list the series name twice? (as evidenced by selecting different index numbers on what appears to be the same series of unique values, but getting the same answer)


Solution

There is no row that is "cut", nor jump to a next value. Indexing is always reproducible, you might misunderstand how it is working.

test3[x] gives you the value y that is indexed by the indice x:

In the case of test3[test3[2]], first test3[2] is evaluated. 2 is in the index and the matching value is 3. Coincidentally, 3 as a value is also in the index, so test[test3[2]] is equivalent to test3[3] and returns 4.

If your Series was test3 = pd.Series([6, 7, 8, 9]) you would just get an error running test3[test3[2]] as test[8] is inexistent.



Answered By - mozway
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing