Jossy
Jossy

Reputation: 1021

Should I define Pandas DataFrame column names as string variables?

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.

Upvotes: 5

Views: 1067

Answers (1)

Igor Rivin
Igor Rivin

Reputation: 4864

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].

Upvotes: 2

Related Questions