I'm trying to calculate the mean of data from a csvfile in python. When I put in code, it is returning an attribution error. This is my code:
import csv
import os
userhome = os.path.expanduser('~')
csvfile = userhome r'/Desktop/Week2Milestone.csv'
with open(csvfile, 'r') as data:
for line in csv.DictReader(data):
print(line)
import numpy as np
child_mean = np.mean(csvfile.childs)
This is the error I receive:
child_mean = np.mean(csvfile.childs)
AttributeError: 'str' object has no attribute 'childs'
CodePudding user response:
Actually csvfile is the string of the path of the file not the file itself. I think you should use a csv_reader, iterate in a for each to get the values and then do the mean. To give an idea:
with open(x) as file:
csv_reader = csv.reader(file,
delimiter=',')
for row in csv_reader:
Row[column] #you are accessing to the cell