1

I'm considering changing all my DataFrameA.columnA to DataFrameA["columnA"] because it looks like the docs use the bracket syntax quite often. It looks like better practice because it offer the opportunity to dynamically pick a column based on a variable instead of hard coded. For example, you could do:

columnWanted="columnA"; DataFrameA[columnWanted] # Yield ColumnA, GOOD 

With the other syntax,

columnWanted="columnA"; DataFrameA.columnWanted # Yields Nothing, BAD. No way of Evaluating Variable. 

would not work. Because it looks for "columnWanted" and there's no way you can put some sort of statement that you want columnWanted to be evaluated for it's value in python.

https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html

2
  • I don't believe there should be a period between DataFrameA and ["columnName"]. Commented Jan 5, 2021 at 16:17
  • @JohnGordon You're right Commented Jan 5, 2021 at 16:29

2 Answers 2

1

You can use both above conventions as long as:

  • the column in question exists,
  • the column name is a string literal, not a variable holding a string.

There is however another limitation to the usage of the attribute notation, namely the column name must be a valid identifier (e.g. it can not contain any space).

But if you create a new column, then the only choice is the bracket notation.

In my opinion, the attribute notation is used quite often and there is no need to change to the bracket notation only to use a single notation only.

Note also that df.xxx is more concise than df['xxx'], so I prefer rather the attribute notation.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all it should be DataFrameA["columnA"] and not DataFrameA.["columnA"].

Also, here:

columnWanted="columnA"; DataFrameA.columnWanted # Yields Nothing, BAD. No way of Evaluating Variable. 

"No way of Evaluating Variable" is not exactly true. You can replace it with

columnWanted="columnA"; getattr(DataFrameA, columnWanted) # equivalent to DataFrameA.columnA 

but you should not use this here. It's better practice to use DataFrameA["columnA"].

About the question, both are exactly the same. you can see this by running

DataFrameA.columnA is DataFrameA["columnA"] 

The output is True

1 Comment

Didn't know about getattr. Looks like that will come in handy alot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.