Should I define Pandas DataFrame column names as string variables?

Viewed 329

I'm working with pandas DataFrames and I was wondering what the convention was for referring to column names. Should I define column names as string variables at the top of a cell? That way if I ever decide to change a column name I only need to change the variable assignment?

Looked through PEP 8 here but couldn't see any guidance.

1 Answers

Your solution is a fine one, but beware. If you have a column called "foo", and define col = "foo", then, although you can refer to the column by df.foo, you can not refer to it as df.col, and have to use df[col].

Related