Pands label selection methods confusion

See above, df[[“col1”, “col7”]] semantically is very similar to df[“row3”:“row5”], because “row3”:“row5” is kind equivalent to [“row3”, “row4”, “row5”], but the results are very different, one is for column selection and the other is for row selection, which creates some confusion, what do you think?

1 Like

Hi @skywalker,

Agreed! The shortcuts can be confusing. However, I am not sure whether shortcuts can be created semantically. This is because of the limited options we have when it comes to creating a shortcut. For example, Ctrl + C is a shortcut for Copy. Which makes sense cause C is the first letter of Copy. But why paste is Ctrl + V instead of Ctrl + P. First of all P is reserved for Print. So they must come up with a new key combination. But why did they choose V? Because it appears next to C in keyboard and from a usability standpoint, we may want to perform both Copy and Paste in a sequence. So V makes it easier. Therefore, shortcuts are often created based on usability and intuition.

>>> import seaborn as sns
>>> iris = sns.load_dataset('iris')

In a dataset, we use slice operation mostly when we want to select rows. And the row indices are most often arranged in ascending order. So it is very intuitive to do:

>>> iris[5:10]
   sepal_length  sepal_width  petal_length  petal_width species
5           5.4          3.9           1.7          0.4  setosa
6           4.6          3.4           1.4          0.3  setosa
7           5.0          3.4           1.5          0.2  setosa
8           4.4          2.9           1.4          0.2  setosa
9           4.9          3.1           1.5          0.1  setosa

On the other hand, often, we may only want to select a specific list of columns. So this shortcut works well:

>>> iris[['petal_length', 'species']]
     petal_length    species
0             1.4     setosa
1             1.4     setosa
2             1.3     setosa
3             1.5     setosa
4             1.4     setosa
5             1.7     setosa
6             1.4     setosa
7             1.5     setosa
...

Hope this helps :slightly_smiling_face:

Best,
Sahil