1

This is a very basic question, but I believe it’ll help someone out there (including me).

When a data frame is created and we want to reference the column name, we can do so by the following:

df[“Column Name Example”]

However, we can also reference a column name by the ‘dot’ method, such as this example:

df.column_name_example

The second option works because of the underscores within the column name. What if the column name has spaces? How do we use the ‘dot’ method to reference column names?

mozway
  • 194,879
  • 13
  • 39
  • 75
HelpMeCode
  • 299
  • 2
  • 13
  • Why should it matter whether you are reading using indexing, or dot reference?? If that's what you want, just rename the columns replacing space by underscore, then you are free to use dot reference. – ThePyGuy Aug 25 '22 at 04:26
  • @ThePyGuy are you saying it’s impossible to use the dot method for a column name that has spaces?? – HelpMeCode Aug 25 '22 at 04:28
  • In R, you can wrap the name in ‘ ‘ symbols to reference a column name with spaces, but that doesn’t seem to work in Python. – HelpMeCode Aug 25 '22 at 04:31
  • The 'dot' notation is available to access __only__ the columns whose names are legal Python identifiers. – DYZ Aug 25 '22 at 04:38
  • This is impossible. Btw, for other reasons (such as clash with pandas methods), using attribute access (dot method) is quite unrecommended. – mozway Aug 25 '22 at 04:39

1 Answers1

2

This is not possible, and not due to pandas design but rather to python's syntax.

You can only use valid python names as attribute.

  • it must start with a letter or underscore
  • it can only contain letters, 0-9, and _

other limitations

Other than this limitation, this method of indexing is highly unrecommended, as it is not programmatic and can lead to bugs if the name clashes with a pandas method.

Example:

df = pd.DataFrame({'sum': [1,2,3]})

df.sum + 1

Output:

TypeError: unsupported operand type(s) for +: 'method' and 'int'

In contrast:

df['sum'] + 1

0    2
1    3
2    4
Name: sum, dtype: int64

In summary, you should really avoid using attribute access, only doing it for quick testing if you want to spare 3 keystrokes...

mozway
  • 194,879
  • 13
  • 39
  • 75