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

Cameron Simpson cs at cskk.id.au
Wed May 3 22:29:45 EDT 2023


On 03May2023 17:52, Artie Ziff <artie.ziff at gmail.com> wrote:
>The code came from a video course, "Pandas Data Analysis with Python
>Fundamentals" by Daniel Chen.
>
>I am curious why the author may have said this. To avoid attaching
>screenshots, I'll describe this section of the content. Perhaps someone can
>say, "oh that's how it used to work"... haha

Unlikely; Python indices (and by implication Pandas indices) have 
counted from 0 since forever.  I suspect just a typo/braino.

>D.CHEN:
>"You can also subset the columns by number. If we wanted to get the first
>column from our data set, we would use zero":
>
>df = pandas.read_csv('./data/gapminder.tsv', sep='\t')
>>>> subset = df[[0]]
>>>> print(subset.head())
>       country
>0  Afghanistan
>1  Afghanistan
>2  Afghanistan
>3  Afghanistan
>4  Afghanistan
>
>Data for the course:
>https://github.com/chendaniely/pandas_for_everyone.git
>
>"df[[0]]" is being described to the course student as selecting the first
>column of data.  :-)

Well, I would say it makes a new dataframe with just the first column.

So:

     df[ 0 ]     # spaces for clarity

would (probably, need to check) return the Series for the first column.  
versus:

     df[ [0] ]   # spaces for clarity

makes a new dataframe with only the first column.

A dataframe can be thought of as an array of Series (one per column).

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list