Hi All,
I'm currently using a search cursor to read data from a feature class in a Jupyter Notebook ArcGIS Pro. I'd like to migrate out of ArcGIS Pro and use a simple csv reader to return a value from a function.
Basically, I need to loop through a list of locations ('Nodes'), and find the row for that location, then calculate the value of a function based on the values of other columns in that row.
While I have a search cursor that works as intended, I'm unsure how to loop through the csv and reference the value of one column based on the value of another column.
The search cursor looks like this:
def Function2GetAValue(Node,Prop,Time):
fields = [LIST OF FIELDS HERE]
with arcpy.da.SearchCursor(f'FeatureClass{time},fields) as cursor:
for row in cursor:
if Node == row[0]:
Value = ((Prop+ row[1] + row[2]) * row[3])
return (round(Value,2))
What I'd like to have is something closer to this:
import pandas a pd
GETaValueFromFC = fr"PATHTOTHEFEATURECLASSTABLE{time}.csv"
dfT=pd.read_csv(GETaValueFromFC)
for index, row in dfT.iterrows():
if ['Node'] == currentNode:
DM = ((Prop + row[COLUMname] + row[COLUMname]) * row[COLUMname])
return (round(float(VALUE),2))
Solved! Go to Solution.
Hey @Ashleigh_Price
Pandas is a great tool to use for this, I use it extremely often, almost daily to this exact thing, here is your code rewritten:
import pandas as pd
def get_value_from_csv(node, prop, time):
csv_path = fr"PATHTOTHEFEATURECLASSTABLE{time}.csv"
df = pd.read_csv(csv_path)
matching_row = df[df['Node'] == node]
if not matching_row.empty:
row = matching_row.iloc[0]
value = ((prop + row['Column1'] + row['Column2']) * row['Column3'])
return round(float(value), 2)
else:
return None
# Example:
# current_node = "some_node_value"
# property_value = 10
# time_period = "2025"
# result = get_value_from_csv(current_node, property_value, time_period)
Cody
Need to learn Pandas.
import pandas as pd --I am just a newb here at esri. If I am wrong, someone, please tell me.
Hey @Ashleigh_Price
Pandas is a great tool to use for this, I use it extremely often, almost daily to this exact thing, here is your code rewritten:
import pandas as pd
def get_value_from_csv(node, prop, time):
csv_path = fr"PATHTOTHEFEATURECLASSTABLE{time}.csv"
df = pd.read_csv(csv_path)
matching_row = df[df['Node'] == node]
if not matching_row.empty:
row = matching_row.iloc[0]
value = ((prop + row['Column1'] + row['Column2']) * row['Column3'])
return round(float(value), 2)
else:
return None
# Example:
# current_node = "some_node_value"
# property_value = 10
# time_period = "2025"
# result = get_value_from_csv(current_node, property_value, time_period)
Cody
Solved! This works just like the search cursor (and is much faster).
Thank you!