Obtaining dataframe values¶
Watch it
See the accompanied youtube video at the link here.
At this point of the module, we now know how to get a subset of an existing dataframe, but what if we just want to get a single value from it?
cereal.loc[[63]]
name | mfr | type | calories | protein | fat | sodium | ... | sugars | potass | vitamins | shelf | weight | cups | rating | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
63 | Shredded Wheat | N | Cold | 80 | 2 | 0 | 0 | ... | 0 | 95 | 0 | 1 | 0.83 | 1.0 | 68.235885 |
1 rows × 16 columns
For example, what if we wanted to save the calorie content of Shredded Wheat
by extracting it from the dataframe manually instead of typing
the number in Python?
To do this we use again our .loc
notation and we specify the row we
are targeting which is 63, followed by the column, here calories
. This
goes in the square brackets.
cereal.loc[63, 'calories']
80
When we do this, it displays the the value contained in the cell, which in this case, is 80.
What about if we want just the rating of Smacks
which is located at index
66?
cereal.loc[[66]]
name | mfr | type | calories | protein | fat | sodium | ... | sugars | potass | vitamins | shelf | weight | cups | rating | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
66 | Smacks | K | Cold | 110 | 2 | 1 | 70 | ... | 15 | 40 | 25 | 2 | 1.0 | 0.75 | 31.230054 |
1 rows × 16 columns
Again we use .loc[]
notation, and we specify the row and the column
location separated by a comma.
So here we write cereal.loc
and the inside the brackets we write [66, 'rating']
.
cereal.loc[66, 'rating']
31.230054