Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QST: difference using ['column'] or .column? #41629

Closed
ivangvi opened this issue May 23, 2021 · 6 comments
Closed

QST: difference using ['column'] or .column? #41629

ivangvi opened this issue May 23, 2021 · 6 comments
Assignees
Labels
Docs good first issue Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@ivangvi
Copy link

ivangvi commented May 23, 2021

Hi All,

I was wondering if you have any guidelines as to when it is possible to just go for df.column instead of df['column'], as I prefer the former for code styling, but I recall it gave me some issues before. I'm not sure I understand the specific difference between one and the other, is there any?

For now, what I do is only use ['column'] when declaring a new column and then always go for .column.

Thank you,
Iván.

@ivangvi ivangvi added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels May 23, 2021
@pablodz
Copy link

pablodz commented May 23, 2021

Hello there,

There is a external blog that details the differences and why or why not to use one of them

https://medium.com/dunder-data/select-a-single-column-of-a-pandas-dataframe-with-the-brackets-and-not-dot-notation-a5ec981cbae6

If you can't see the post, open the link in incognito mode

Basically:

df['my_col'] is better than df.my_col

@jreback
Copy link
Contributor

jreback commented May 23, 2021

that's a nice post
would take a PR to link it in the docs

@mzeitlin11 mzeitlin11 added Docs good first issue Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels May 23, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone May 23, 2021
@LukeTonin
Copy link

take

@ivangvi
Copy link
Author

ivangvi commented May 23, 2021

Thank you @ZurMaD!
It looks like no.5 was what confused me in the first place:

image

And now I understand why I was confused. I guess my way of using [] when declaring new columns and then .column as much as possible, works just fine... all the other points mentioned I already knew about.

@LukeTonin
Copy link

LukeTonin commented May 23, 2021

Hi all,

I'm thinking of adding a line to the Attribute access section of the Indexing and selecting data page of the documentation.

Unless I'm told otherwise, I'll probably go with:

While attribute access will work in many cases, bracket indexing is the recommended syntax for indexing a DataFrame. For 10 reasons why, please check this post about attribute vs bracket indexing.

@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 25, 2021
@mroeschke
Copy link
Member

As mentioned in #41645 (comment), this is already documented in https://pandas.pydata.org/pandas-docs/dev/user_guide/indexing.html#attribute-access. Closing

@mroeschke mroeschke removed this from the 1.3 milestone May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs good first issue Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants