why df.iloc**( )** does not work, why does it have to be [. ]. would appreciate if someone could dive into the ‘grammar’ of operator [ ] (if you could please )
another real quick question. I am doing a side project, there’s a column name ‘shape’, here’s my syntax:
df[df.shape == ‘triangle’]. system reports error because DataFrame actually has an attribute named shape. so how do I go around it?
Like most of the indexing tools in Python (lists, strings, dictionaries etc.) the subscript syntax [] is used for accessing elements which actually calls the __getitem__ dunder method. There are some mild benefits to using this in Pandas objects like the support of slice notation but I think it’s mainly to be consistent with normal Python. I don’t know that this is the reason but I think it might be.
If you have a column with a label that matches an attribute of the dataframe (inherited or otherwise) then you must use the ["label"] syntax to access that column. The use of the . dotted attribute lookup is just a convenience method implemented by Pandas, the [] is more robust. For a little more info- https://discuss.codecademy.com/t/faq-modifying-dataframes-review/373659/34
check the indentation of the line of code that the error message is mentioning. (in other words, the system is telling you you put too much space at the beginning of at least one of your lines of codes.)