Reputation: 3
I am doing a project for my CSCI101 class at college, was thinking of using a CSV file to look at data for baseball players, like batting average, strikeout percentage, WAR, etc. to determine who should be player of the week using those numbers. Is there a way to look at data line by line in a CSV file? Like if a line of the CSV is "PlayerName, .247, .421, 22.4" and I want to save each of those numbers to a separate variable using the column headers. I'm pretty new to coding so sorry if the question doesn't really make sense. Thank you.
Upvotes: 0
Views: 240
Reputation: 446
the package pandas is probably what you need here. Specifically, the function read_csv will let you create directly a dataframe (structured data structure that represents a table) storing your data.
You can then do all the analysis you need :)
Upvotes: 0
Reputation: 164
You could turn it into a list which is the easiest way.
import csv
data=[]
with open(filename,'r') as f:
reader = csv.reader(f)
for line in reader:
data.append(line)
Now the variable data holds each line. To get the player name of the ith line, do data[i][0]
. I hope this helps, I don't totally understand what you mean by variables using column headers. If you mean having each column feed a list, then just turn data into each list through comprehension. For example, to make the list of player names do player_names = [line[0] for line in data]
which takes the first item in each line
Upvotes: 2