Python Introduction to pandas Meet pandas Accessing a DataFrame

Arturo Acosta
Arturo Acosta
7,307 Points

I'm playing around with accessing methods and i realised something

1) So first we know we can access a full column by it's name as the notebook says on the third and fourth cells. 2) Then we can access a full row by label such as the 7th and 8th cell explain. So far so good. 3) We see in the 9th and 10th cell that we can also access a full row by its numeric index using iloc[index].

The question is, i am trying to access a full column by index, i tried iat[], which is stated in the pandas dataframe docs but it won't allow it, i would also need a row index to acces a single value and not the full column. Is there anyway we can acces a full column by index just like we did with iloc, that doesn't involve transposing or changing the dataframe format?

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 24,835 Points

I am not a huge fan of the "ugly" syntax, but suppose you wanted to get the column with index 0

dataframe.iloc[ : , 0]

see the example

import pandas as pd
dataframe = pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/mlb_teams_2012.csv")
print("======== show the head =============")
print(dataframe.head())
print("======== print Team column =========")
print(dataframe['Team'])
# team is in the zero-th column
col_index = 0
print("========== print column index 0================")
print(dataframe.iloc[ : ,col_index])
Arturo Acosta
Arturo Acosta
7,307 Points

Thanks, I recall trying something like that but i might've had a mistake in the syntax,