Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading CSV column name starting with number (Python)

Tags:

python

pandas

csv

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.

like image 642
Kai Avatar asked Aug 22 '26 13:08

Kai


1 Answers

You can use bracket notation:

doubles = df['2B']
triples = df['3B']
like image 84
Mr.F Avatar answered Aug 25 '26 03:08

Mr.F