Here's how to do indexing with Pandas.
Pretend with have this DataFrame called snakes.
Pretend with have this DataFrame called snakes.
type speed lifespan toxicity 0 cobra 1 2 4 1 viper 4 5 1 2 python 7 8 9
Indexing By Column
Using Column Names
# just one column snakes['speed'] # multiple columns snakes[['speed', 'lifespan']]
Using Column Numbers
snake_columns = snakes.columns # just one column speed_col = snake_columns[0] snakes[speed_col] # multiple columns speed_and_lifespan_cols = snake_columns[0:2] snakes[speed_and_lifespan_cols]
Indexing By Row
Using Row Numbers
# just one row snakes.iloc[1] # this is the viper row # multiple rows snakes.iloc[[0,2]] # this is the viper row and the python row
Using Row Names
To index by row names, you'll need to set the index first. By default, you'll usually just have an index of integers. Instead of using .iloc, which is for indexing by row number, we're going to use .loc.
# set the index snakes = snakes.set_index('type') # just one row snakes.loc['viper'] # multiple rows snakes.loc[['viper', 'python']]