0

I have a CSV file with zero as some of the attribute values. Trying to use cursors and replace all zeros with no data or blank values. I am able to replace any numbers. But not able to update with NULL or no data. The field data type is integer. Tried with Search and Insert cursor, also tried converting to DBF table and then replace zero with null values. Did not work as it seems it is considering null as string. Below is the code

import arcpy
import csv

path= "pm2.5.csv"
fieldObs=arcpy.ListFields(path)
fieldNames= []

for field in fieldObs:
    fieldNames.append(field.name)
print(fieldNames)

updateRows = arcpy.da.UpdateCursor(path, fieldNames)
for updaterow in updateRows:
    if updaterow =="0"
       updaterow is None
    curU.updateRow(updaterow)
del updateRow, updateRows
4
  • 3
    CSV and dbf have no concept of NULL, you need to import your data into a proper database table if you want to store NULL values in your data. Suggest you create a file geodatabase and import your table into that.
    – Hornbydd
    Nov 16, 2021 at 22:09
  • A SearchCursor is read-only. I'm amazed that InsertCursor works with CSV at all.
    – Vince
    Nov 16, 2021 at 22:15
  • o. thank you for the clarification.
    – Sara
    Nov 17, 2021 at 23:15
  • Can we search for zero values using search cursor and replace zero with "no data" or blank values?
    – Sara
    Nov 17, 2021 at 23:19

1 Answer 1

1

Do you have a particular reason to use cursor to update a csv ? It's not the right tool for the job if you ask me.

Instead, you could try to use the pandas module. It comes natively with the Arcmap python installation, so no installation required.

import pandas as pd

df = pd.read_csv("pm2.5.csv")
df = df.replace(0, '')  # Replace 0 with blank values
df.to_csv("pm2.5_nan.csv")  # Save to a new file

You could also import numpy as np and change the replace line with df = df.replace(0, np.nan). However, NaN are considered as float values in python so all your values would be converted to float values instead of integers. When saving to a csv, the NaN will be converted to blank values.

More info on the read_csv function : https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

1
  • Yes. thats correct. i was able to run code successfully with pandas. But using cursor is a requirement.
    – Sara
    Nov 17, 2021 at 23:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.