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

Artie Ziff artie.ziff at gmail.com
Wed May 3 20:52:00 EDT 2023


I agree with your analysis, Cameron.

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

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.  :-)

I'll study that link.
Thank you.


More information about the Python-list mailing list