Pandas questions: dot notation vs brackets?

I've noticed we use dot notation when accessing columns in pandas (vs brackets).  Any special recommendations here? Pro and cons of each method? 



-Thanks



PS: MRS course / Ernie Chan

Hello Alex,



It is recommended that you use brackets as the dot format won't be able to help you when there is a space in the column name.



For example, df['price'] and df.price will work fine but if we have a column name like 'adj close' then we can access it only using the bracket format (df['adj close']). The dot format df.adj close will give us a parse error.



 

Yes, I also recommend the bracket notation. For the above reason + the fact that the bracket notation is the base method to access columns in Pandas. When you use a bracket notation, it internally translates to the getitem method call. The dot notation is a syntactic sugar - it triggers the getattr method, which in turn will invoke the getitem. So bracket access will be in general marginally faster than the dot access. Although for most cases the difference in performance will be negligible. On the other hand, the good thing about the dot notation is that it makes the code more readable.