index in pandas code example

Example 1: pandas index

How to add index to the DataFrame at runtime

df["patient"] = ["patient-" + str( i ) for i in range( max( df.index ) + 1  )]
df.set_index( "patient", inplace=True )
---------------------------------------------------------------------                 
patient = pandas.Series( ["patient-" + str( i ) for i in range( max( df.index ) + 1 )] )
df.index = patient
---------------------------------------------------------------------
df.index = ["patient-" + str( i ) for i in range( max( df.index ) + 1 )]

Example 2: how to index python

#you can index in a string like:
string='cupcake'
string[0]#returns 'c'
#you can index in a list like:
list = ['red', 'blue', 'green']
list[0]#returns 'red'
list[0][0]#returns[0] of 'red' wich is r

Example 3: df.index

The index (row labels) of the DataFrame.

Example 4: indexing column in pandas

In [10]: type(titanic[["Age", "Sex"]])
Out[10]: pandas.core.frame.DataFrame

Tags:

Misc Example