.mean() on column number?

How come when you you do the .mean() function in pandas on a dataframe column, you have to input the column name, and you can’t input the column number?


star_wars[“column_2”].mean() = GOOD
star_wars[1].mean() = BAD


Hey, Matt.

I can’t give you a full-fledged answer right now. But here’s something to think about: what if, say, the tenth column’s name is 1? Is star_wars[1] the second column or the tenth?

Also note that the question in the topic’s title really isn’t the issue here, you just want to ask about how to refer to columns.

Can you please take a look at this guide on how to ask a question and edit your post accordingly?


What you want to do is access the column by index. You can do that with DataFrame.iloc:

star_wars.iloc[:, 1].mean()


1 Like

Ahhh, that makes sense. Thank you!!