Doubt in pandas


Please correct me if my understanding is wrong
In the below code, what it will do is it will destroy if any variable present
by the name electoral_votes and create a new variable with name electoral_votes

electoral_votes = electoral_votes.set_index('State')

Electoral votes is the data frame (think of it as a table). Inside this there are columns - state and votes. This table has a default index which goes from 0 to 1. Using The above command what happens is, instead of using the default index, it starts using the values of state column as index instead. So now your new index is not 0,1,2… it is the values of the state column.

Screen Shot 2020-07-15 at 6.03.37 PM

See… index is now 0,1,2… state and votes are columns…

After running the command -

Screen Shot 2020-07-15 at 6.04.52 PM

0,1,2… index is gone. State column is shifted slightly if u see… it is acting as new index.


Hi Anshuman,

Thanks for the clarification,

Thanks and Regards,