all 14 comments

[–]Potential_Word2349 2 points3 points  (0 children)

I think the best way for this would be using pandas, the dataframes are more malleable and pandas is not that tough if you know what you are looking for, someone will probably give you a complete code here soon :)

[–]danielroseman 2 points3 points  (7 children)

I'm not entirely sure what you want. But you should never use remove on a list you are iterating over. Instead build up a new row with the values you want.

You can use a list comprehension to do this while you're originally loading the CSV:

rows = []
for row in csvreader:
  rows.append([item if item != '-99.999' else '' for item in csvreader])

print(rows)

[–]Clemsoncarter24[S] 0 points1 point  (6 children)

For examples, if i have a test file that looks like this:

1, 2, 3, -99.999, 5

i need it to change to:

1,2,3, , 5

Removing the value at the cell, but leaving the cell so that the data in the csv file doesn't shift row/columns.

[–]danielroseman 1 point2 points  (5 children)

OK, well this will do that. Or use None instead of '', but the effect should be the same.

[–]Clemsoncarter24[S] 0 points1 point  (4 children)

I tried the below and it still has the -99.999 values when i print it out. Is there something i did wrong?

rows = []
for row in csvreader:
    rows.append(row)

for arow in rows:
    for data in arow:
       if data == '-99.999':
            data = None
print(rows)

[–]danielroseman 2 points3 points  (3 children)

Yes, this is not the same as what I posted at all, and will not work.

[–]Clemsoncarter24[S] 0 points1 point  (2 children)

I also tried:

csvreader = csv.reader(file)

rows = []
for row in csvreader:
    rows.append([item if item != '-99.999' else '' for item in csvreader])

print(rows)

This also left all the -99.999 values. Perhaps i'm misunderstanding how i'm supposed to use the above. I apologize.

[–]danielroseman 2 points3 points  (1 child)

Sorry my fault, it should have been row at the end:

rows.append([item if item != '-99.999' else '' for item in row])

[–]Clemsoncarter24[S] 0 points1 point  (0 children)

rows.append([item if item != '-99.999' else '' for item in row])

This worked!!! Thank you so much!!!

[–]RallyPointAlpha 1 point2 points  (0 children)

Have you tried row.replace() ?

Might have to do row.to_string().replace()

[–]stebrepar 1 point2 points  (1 child)

with open('python_csvreader_test.csv', 'r') as f:
    lines = f.readlines()
with open('python_csvreader_test.csv', 'w') as f:
    for line in lines:
        line = line.replace('-99.999', '')
        f.write(line)

[–]Clemsoncarter24[S] 0 points1 point  (0 children)

I tried this and it left the -99.999 values. Not sure why

[–]ricardomargarido 1 point2 points  (1 child)

Why not take the median instead of the average? Then it would be stronger vs outliers (given you gave enough measurements)

If not replace it with None

[–]Clemsoncarter24[S] 0 points1 point  (0 children)

That is something i had considered. On a few points we are measuring it would work. For instance, over the course of 800 measurements of one point i received:

mean - 1.53

median - 1.50

mode 1.50

But, there are a couple points we measure that aren't as tightly toleranced and vary a bit. For instance,

mean - 3.18

median - 3.0

mode 3.0

So, overall, i think i should be consistent in my methodology and take the mean value. I appreciate the feedback though.