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
-
3CSV 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– Hornbydd2021年11月16日 22:09:46 +00:00Commented Nov 16, 2021 at 22:09
-
A SearchCursor is read-only. I'm amazed that InsertCursor works with CSV at all.Vince– Vince2021年11月16日 22:15:30 +00:00Commented Nov 16, 2021 at 22:15
-
Can we search for zero values using search cursor and replace zero with "no data" or blank values?Sara– Sara2021年11月17日 23:19:05 +00:00Commented Nov 17, 2021 at 23:19
1 Answer 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
-
Yes. thats correct. i was able to run code successfully with pandas. But using cursor is a requirement.Sara– Sara2021年11月17日 23:16:53 +00:00Commented Nov 17, 2021 at 23:16