graphing columns from a CSV that includes null values- python -
i relatively new python , running lot of issues. trying create graph using 2 columns csv file contains many null values. there way convert null value 0 or delete row contains null values in columns?
your question asked underspecified, think if pick concrete example, should able figure out how adapt actual use case.
so, let's values either string representation of float, or empty string representing null:
a,b 1.0,2.0 2.0, ,3.0 4.0,5.0
and let's you're reading using csv.reader
, , you're explicitly handling rows 1 one do_stuff_with
function:
with open('foo.csv') f: next(reader) # skip header row in csv.reader(f): a, b = map(float, row) do_stuff_with(a, b)
now, if want treat null values 0.0, need replace float
function returns float(x)
non-empty x
, , 0.0
empty x
:
def nullable_float(x): return float(x) if x else 0.0 open('foo.csv') f: next(reader) # skip header row in csv.reader(f): a, b = map(nullable_float, row) do_stuff_with(a, b)
if want skip rows contain null value in column b, check column b before doing conversion:
with open('foo.csv') f: next(reader) # skip header row in csv.reader(f): if not row[1]: continue a, b = map(nullable_float, row) do_stuff_with(a, b)
Comments
Post a Comment