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
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 16, 2021 at 22:05
3
  • 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. Commented Nov 16, 2021 at 22:09
  • A SearchCursor is read-only. I'm amazed that InsertCursor works with CSV at all. Commented Nov 16, 2021 at 22:15
  • Can we search for zero values using search cursor and replace zero with "no data" or blank values? Commented 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.