double bracket integer index in pandas; Is this a legal syntax

Artie Ziff artie.ziff at gmail.com
Wed May 3 06:41:26 EDT 2023


Hello,

I am hope that pandas questions are OK here.

In a panda lecture, I did not get the expected result.

I tried this on two different platforms
(old macOS distro and up-to-date Ubuntu Linux distro, 22.04)

The Linux distro has:
python                    3.10.11
pandas                    1.5.2
conda                     23.3.1

Is this double bracket form, df[[1]], deprecated... maybe?

There is data in a dataframe, df.

>>> subset = df[[1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/home/dks/anaconda3/lib/python3.10/site-packages/pandas/core/frame.py",
line 3811, in __getitem__
    indexer = self.columns._get_indexer_strict(key, "columns")[1]
  File
"/home/dks/anaconda3/lib/python3.10/site-packages/pandas/core/indexes/base.py",
line 6113, in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)
  File
"/home/dks/anaconda3/lib/python3.10/site-packages/pandas/core/indexes/base.py",
line 6173, in _raise_if_missing
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Int64Index([1], dtype='int64')] are in the [columns]"

What could be making this fail?

Thank you!
az


More information about the Python-list mailing list