TheBetterView
TheBetterView

Reputation: 15

Need a detail understanding about the slicing based on the below example

x = boston_housing.iloc[:,0:3]

Please explain why we need the , and how it slices the dataframe.

Upvotes: 1

Views: 34

Answers (1)

JANO
JANO

Reputation: 3076

I just looked at the pandas documentation for iloc and am surprised that this is not directly explained there. However, there are a few examples where you can see the output of your input.

Basically, in your example, you have two arguments (divided by the ,) for the function iloc. The second one is 0:3, which means that you want to select columns 0 to 3 (exclusive). The first argument is only :, which means you want to select all rows. How slicing works in detail can be seen in this answer.

So, why do we need the first argument if it is simply selecting all rows? It is only a placeholder that we need to write the second argument. It is simply necessary if we only want to specify the columns.

To show you what it does here is an example from the docs. Assuming this dataframe:

      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000

if we use df.iloc[:,0:3] the result is:

      a     b     c
0     1     2     3
1   100   200   300
2  1000  2000  3000

As you can see all rows are still there, but only the first, second, and third column are returned.

Upvotes: 1

Related Questions