0

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.

The code here is what I done so far

The image here shows one of the zeros I want to replace. The zero should be replaced 30.969999

1
  • I'd recommend looking into the pandas package for python. Especially for these kinds of tasks there are many many functions that will do it for you Commented Dec 13, 2019 at 8:03

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

The person wants to replace a zero value with the value before it. That's the question. Isn't it?
1

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

0

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

I was going to answer the same; you should also add a check that we're not already on the first element, because if i == 0, we'd end at [-1], and that's not what we want...
I know, that's what the if i: statement is. If i is anything other than zero, it will return True.
ops, I was looking for it... and still missed it, sorry!
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.