6.5. DataFrame Getitem
6.5.1. SetUp
>>> import pandas as pd
>>>
>>> df = pd.DataFrame([
... {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30},
... {'firstname': 'Bob', 'lastname': 'Blackthorn', 'age': 31},
... {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32},
... ], index=['a', 'b', 'c'])
>>> df
firstname lastname age
a Alice Apricot 30
b Bob Blackthorn 31
c Carol Corn 32
6.5.2. Column Getitem
df['column_name']- get column by labelWorks for any column name
>>> df['firstname']
a Alice
b Bob
c Carol
Name: firstname, dtype: str
6.5.3. Column Loc
df.loc[:, 'column_name']- get column by labelWorks for any column name
>>> df.loc[:, 'firstname']
a Alice
b Bob
c Carol
Name: firstname, dtype: str
6.5.4. Column Attribute
DataFrame.column_name- get column by attributeNot recommended
Works only if column name is a valid Python identifier and does not conflict with existing DataFrame methods or attributes
>>> df.firstname
a Alice
b Bob
c Carol
Name: firstname, dtype: str