How to count rows in a csv file and ignore the rows not in use (using python) -
i have csv file contains height in feet , inches , weight of basketball players. however, of data has null or blank space feet , height.
i have conclude how many players obese based on bmi, calculated height , weight.
i found out there 11 players obese. however, need find percentage of obese players within set of data. having trouble figuring out how find total number of players (ignoring have null or none in row).
here example of data have:
firstname lastname position firstseason lastseason h_feet h_inches weight marquis daniels g 2003 2009 6 6 200 predrag danilovic g 1995 1996 6 5 200 adrian dantley f 1976 1990 6 5 208 mike dantoni g 1975 1975 null null null henry darcey c 1952 1952 6 7 217 jimmy darden g 1949 1949 6 1 170 oliver darden f 1967 1969 6 6.5 235 yinka dare c 1994 1997 7 0 265 jesse dark g 1974 1974 6 4.5 210
you can see rows have null data.
so far have python code:
def read_csv(filename): """ reads comma separated value file, returns list of rows; each row dictionary of columns. """ open(filename, encoding="utf_8_sig") file: reader = csv.dictreader(file) rows = list(reader) return rows # try out function players = read_csv("players.csv") # print information on first player, demonstrate how # data pprint import pprint def is_obese(player): if (player["h_inches"] , player["h_feet"] , player["weight"]) == 'null' or (player["h_inches"] , player["h_feet"] , player["weight"]) == none: pass else: total_h_inches = float(player["h_feet"]) * 12 + float(player["h_inches"]) bmi = (float(player["weight"])/(total_h_inches**2))* 703 return bmi >= 30 count = 0 player in players: if is_obese(player): print ('player', player["lastname"], 'is obese') count = count + 1 else: pass print ("the total number of obese players:", count)
and returns:
player boozer obese player brand obese player catlett obese player davis obese player hamilton obese player lang obese player maxiell obese player miller obese player smith obese player traylor obese player white obese total number of obese players: 11
keep counter total number of players well, , add players counters if there data on them.
# returns true if player has data, otherwise returns false def has_data(player): return (player["h_inches"] != 'null' , player["h_feet"] != 'null' , player["weight"] != 'null' , player["h_inches"] not none , player["h_feet"] not none , player["weight"] not none) obese_count = 0 total_count = 0 player in players: if has_data(player): if is_obese(player): print ('player', player["lastname"], 'is obese') obese_count += 1 total_count += 1
Comments
Post a Comment