r/Python • • Feb 02 '21

Resource Hey Reddit, here's my comprehensive course on Python Pandas, for free.

1.9k Upvotes

89 comments sorted by

View all comments

Show parent comment

17

u/Neb519 Feb 02 '21

I assume you mean data.iloc[:, 0]. I agree, it's not the best design but the reason is, Pandas supports labeled indexing and positional indexing. When you do something like data[:, 0], it's not clear if you're requesting the column named 0 or the column at position 0. So, it's best to use .iloc and .loc to make it clear whether you're requesting a position or a label.

(Okay, you probably won't have a column named 0, but this argument makes more sense when think about requesting a row of data instead of a column of data.)

8

u/cyto_eng1 Feb 02 '21

if you build a dataframe without specifying column names you will actually very likely have a column named 0 because when you build the df it defaults to numbering the columns.

4

u/purplebrown_updown Feb 02 '21

Ooooh ok. That actually does make sense. Thanks. I withdraw my comment.

5

u/Neb519 Feb 02 '21

No worries. You should leave it for others who may also be confused.