Selecting a Single Column¶
Watch it
See the accompanied youtube video at the link here.
Something we often do in data analysis is obtain a single column from
a dataframe. We can again use .loc[]
to do this which would look
something like this in general:
dataframe.loc[:, ['column name']]
So if we here want the column named type
from our cereal dataframe we
could use the syntax:
cereal.loc[:, ['type']]
type | |
---|---|
0 | Cold |
1 | Cold |
2 | Cold |
3 | Cold |
4 | Cold |
... | ... |
72 | Cold |
73 | Cold |
74 | Cold |
75 | Cold |
76 | Cold |
77 rows × 1 columns
This seems a bit long winded and since we do this type of thing often. Luckily, Pandas has provided a quicker syntax to use to do the same thing.
Instead, selecting a single column can be done without using .loc[]
and we can just specify the dataframe name, followed by double square
brackets containing the column of interest (df[['column name']]
).
cereal[['type']]
type | |
---|---|
0 | Cold |
1 | Cold |
2 | Cold |
3 | Cold |
4 | Cold |
... | ... |
72 | Cold |
73 | Cold |
74 | Cold |
75 | Cold |
76 | Cold |
77 rows × 1 columns
This makes the syntax for selecting the column type
from the cereal
dataframe very easy.