Accessing data using indexes in python

Indexes are position numbers of each element in python. Indexes start from zero. These numbers helps to pick values from a given position in a data frame.

Hence, the first element in a data in python will have index=0, the second element will have index=1 so on and so forth.

Indexes in 1D objects

One dimensional(1D) objects can be imagined as a single column. Which have many rows and just one column. For such kind of data if you need to extract a portion of data, then you can specify which values you need using index numbers.

In below example, a 1D list is sliced using different indexing options

Sample Output:

One dimensional indexing
One dimensional indexing

One Dimensional indexing
One Dimensional indexing

Indexes in 2D objects

When you are trying to slice and get a portion of data from a Two dimensional object like a DataFrame, then you need to provide two set of indexes, one for Rows and one for columns. It’s like which rows you need and which columns you need from the data.

Defining a sample data frame below to illustrate the indexing examples

Sample Output:

Sample Employee DataFrame
Sample Employee DataFrame

In the below examples, you can see how to subset a DataFrame by providing the indexes of rows and columns.

Sample Output:

Accessing values from data frame using indexes
Accessing values from data frame using indexes

Accessing Multiple Rows and columns from a DataFrame using indexes
Accessing Multiple Rows and columns from a DataFrame using indexes

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *