Kai Kai - 6 months ago 26
Python Question

Reading CSV column name starting with number (Python)

I had a question about reading in a column name from a CSV file into my python program starting with a number? So my CSV datafile called

Teams.csv
has columns with names or headers such as
R
,
AB
,
H
,
2B
,
3B
, etc. Specifically, the CSV file is the Lahman's Baseball Database's Teams.csv file.

Now when I read this file in Python, I use this:

#imported pandas as pd
df = pd.read_csv("Teams.csv")

#Get stats for offense
R = df.R
AB = df.AB
H = df.H


and these work fine, but when I try to import
2B
and
3B
though

doubles = df.2B
triples = df.3B


these give a syntax error because of Python not having variables start with digits.

Is there a way around this that I can do within Python itself? Or do I have to go into the CSV and rename the headers to lets say
X2B
and
X3B
?

I rather not do the latter, because Lahman's Baseball Database has A LOT of CSV files.

Answer

You can use bracket notation:

doubles = df['2B']
triples = df['3B']