{"id":5940,"date":"2025-02-19T04:47:01","date_gmt":"2025-02-19T12:47:01","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=5940"},"modified":"2025-12-01T02:16:56","modified_gmt":"2025-12-01T10:16:56","slug":"pandas-dataframe-in-python-1-disadvantage-of-using-attributes-to-access-columns","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2025\/02\/19\/pandas-dataframe-in-python-1-disadvantage-of-using-attributes-to-access-columns\/","title":{"rendered":"Pandas DataFrame in Python (1): Disadvantage of using attributes (dot notation) to access columns. Use `[]` (getitem) operator instead"},"content":{"rendered":"\n<p>There are two ways to access columns in DataFrame. The preferred way is by square brackets (indexing into it like a dictionary), while it&#8217;s tempting to use the neater dot notation (treating columns like an attribute), my recommendation is don&#8217;t! <\/p>\n\n\n\n<p>Python has dictionaries that handles arbitary labels well while it doesn&#8217;t have dynamic field names like MATLAB do. This puts DataFrame at a disadvantage developing dot notation syntax while the dictionary syntax opens up a lot of possibilities that are worth giving up dot notation for. The nature of the language design makes the dot notation very half-baked in Python and it&#8217;s better to avoid it altogether<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reason 1: Cannot create new columns with dot notation<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https:\/\/pandas.pydata.org\/pandas-docs\/stable\/indexing.html#attribute-access<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reason 2: Only column names that doesn&#8217;t happen to be valid Python attribute names (say, no spaces) AND a DataFrame that does not have any method with the same name as the column can be accessed through dot notation.<\/h2>\n\n\n\n<p>Take an example of dataframe constructed from device info dictionaries created by the package <code>pyft4222<\/code>. I added a column called <code>'test me'<\/code> to a table converted from the dictionary of device info. The tabe <code>T<\/code> looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"67\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-3.png\" alt=\"\" class=\"wp-image-5943\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-3.png 734w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-3-300x27.png 300w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2025\/02\/image-3-500x46.png 500w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure>\n\n\n\n<p>I tried <code>dir()<\/code> on the table and noticed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The column name <code>\"test me\"<\/code> did not appear anywhere, not even mangled. It has a space in between so it&#8217;s not a valid attribute or variable name, so this column is effectively hidden from the dot notation<br><\/li>\n\n\n\n<li><code>flags<\/code> is an internal attribute of DataFrame and it was not overriden by the data column <code>flags<\/code> when called by the dot notation. This means the <code>flags<\/code> column was also shadowed in (aka hidden to) the dot notation as there were no mangled name for it either<\/li>\n<\/ul>\n\n\n\n<p>Even more weird is that <code>getattr()<\/code> works for columns with non-qualified attribute name like <code>test me<\/code> (despite the dot notation cannot access it because of the lack of dynamic field names syntax yet <code>test me<\/code> doesn&#8217;t show up in <code>dir()<\/code>). <code>getattr(T, 'flags')<\/code> still gets the DataFrame&#8217;s internal attribute <code>flags<\/code> instead of the column called <code>flags<\/code> as expected.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_5940\" class=\"pvc_stats all  \" data-element-id=\"5940\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>There are two ways to access columns in DataFrame. The preferred way is by square brackets (indexing into it like a dictionary), while it&#8217;s tempting to use the neater dot notation (treating columns like an attribute), my recommendation is don&#8217;t! &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2025\/02\/19\/pandas-dataframe-in-python-1-disadvantage-of-using-attributes-to-access-columns\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_5940\" class=\"pvc_stats all  \" data-element-id=\"5940\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10,34],"tags":[],"class_list":["post-5940","post","type-post","status-publish","format-standard","hentry","category-matlab","category-python"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=5940"}],"version-history":[{"count":8,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5940\/revisions"}],"predecessor-version":[{"id":6715,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5940\/revisions\/6715"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=5940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=5940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=5940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}