Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introduction to pandas Meet pandas Accessing a DataFrame

Arturo Acosta
Arturo Acosta
7,638 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 28,722 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,638 Points

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