I'm trying to learn more about csv maliputation in python and created a csv file. What I'm trying to figure out is how to replace a zero value with a value before it.
4 Answers
Something along the lines of this should work, if you are willing to install pandas
(pip install pandas)
import pandas as pd
D = pd.read_csv("Stock....")
D = D[["desiredColumn"]].replace(0, pd.np.nan )
D = D[["desiredColumn"]].fillna(method="ffill")
In this example all zeros are first replaced with NAN. Once this is done you can use the fillna function that will fill all NAN. This function has a convinience function to either forward fill (ffill) or backward fill (bfill) all NANs
1 Comment
This is not something specific to csv's. You can do it easily as below.
import csv
with open("input.csv") as file:
reader = csv.reader(file)
for row in reader:
res = [] # load this list with the sanitized `values`
for v in row:
res.append(v if float(v) else res[-1]) # important! first element of `row` must be non-zero
print(res) # use `res` instead of `row`
As noted, this assumes each row starts with a non-zero value.
If this is not guaranteed, a simple solution would be initializing res list with a fallback value for starting zeros then discard the first element of the res once it is loaded.
I.e.
import csv
with open("input.csv") as file:
reader = csv.reader(file)
for row in reader:
res = [99] # if first value of `row` is zero, consider it as `99`
for v in row:
res.append(v if float(v) else res[-1]) # important! first element of r must be non-zero
res = res[1:] # discard the first element after `res` is loaded
print(res)
Comments
If you want to do it in csv module no matter what, then you can do it by just adding a few lines to your if-else statement. Just add:
for row in range(len(reader)):
for i in range(len(reader[row])):
if (reader[row][i] == '0'):
if i: #Check if there's a number before the occurrence of 0
reader[row][i] = reader[row][i-1]
else:
pass
Otherwise, if you aren't really particular with your module, then try using pandas, which is a much more powerful package than the csv package.
3 Comments
if i: statement is. If i is anything other than zero, it will return True.You should transfer the data to its own data structure. The default choice would be a list, unless you want to use it with a library that needs something else input.
import csv
values = list()
with open('file.csv') as file:
reader = csv.reader(file)
for row in reader:
value = int(row[1])
if value == 0:
if values:
values.append( values[-1] )
else:
# Whatever is meaningful in this case
pass
else:
values.append( value )
# do stuff with values here
PS: You make answerers' lives easier if you paste your code as code and not as a screenshot of code. This might make the difference of getting an answer or not in one of your future questions.
1 Comment
int(row[1]) will crash if row[1] is a string representing a floating point. e.g. 1.234
