Dataframes, Variables, & Attributes

I am currently working my way through Mastering Statistics w/ Python, and I often come across the following syntax in the exercises: dataframe.columnvariable.

In the above example, I am not understanding how we are able to use the column variable as an attribute to the data frame. The only exposure I have had to using attributes in this way is when they are pre-defined in a class; however, it does not seem obvious to me that this would be the case for a given data frame and the variables that make up its columns.

For a concrete example, refer to this exercise here. In it, I am tasked to separate out scores for students who live in urban and rural locations. The scores are kept under the column variable G3, and the student addresses are stored in the column associated with the addresses variable. The code below is an example of using column variables as attributes

scores_urban = students.G3[students.address == 'U']

scores_rural = students.G3[students.address == 'R']

Again, I am not understanding what is happening here with the students.G3 and students.addresses. If someone could please explain, I would greatly appreciate it!

Also, here is the first 5 rows of the data frame for reference:

It’s a convenience tool implemented by Pandas. The actual syntax is still the standard dataframe["column"]. In some cases you simply cannot use the dot syntax (when valid attributes with that name exist, for some non-string types), in others some folks consider it simpler/more readable. So the [column] style is more robust but you will see both and may prefer to use one or the other at certain times.

If you have a wee web search for the dot syntax vs. bracket syntax (or attribute vs. subscript syntax) for pandas you’ll find some useful discussion and you can decide when to use those options for yourself.

I’ve not checked the code but I assume they’ve added a __getattr__ dunder method or something very similar to the DataFrame type which operates when no actual attribute can be found.


Thank you for this clarification!

1 Like