0

When looping through a list of Geopandas geodataframes and attempting to get the index value of the current dataframe in the loop, I'm getting ValueError: Can only compare identically-labeled DataFrame objects on the second pass through the loop. I'm not purposefully comparing anything. Why is this error being thrown?

from pandas import read_csv
from geopandas import GeoDataFrame
from cartoframes.utils import decode_geometry

data = '/<pathtomydata>/data.csv'

# read dataset into a pandas dataframe separated into chunks (aka a 'textFileReader' object)
reader = read_csv(data, chunksize=10)

# loop through chunks, turning each chunk into a geopandas geodataframe, and add them all to a list
geo_chunks = []
for df_chunk in reader:
    gdf = GeoDataFrame(df_chunk, geometry=decode_geometry(df['the_geom']))
    geo_chunks.append(gdf)

# loop through the list of geodataframes
for gdf in geo_chunks:
    print(geo_chunks.index(gdf)) # <-- ValueError occurs here (2nd time through loop)
    <do more stuff>
0

1 Answer 1

1

You are comparing the current iteration of gdf to all of the gdfs in the geo_chunks list. list.index(val) compares val to all list elements and pandas will only allow comparisons between dataframes with the same indexes.

It's not relevant to this question, but if you really did want to compare two dataframes, you would use the DataFrame.equals(other) method.

What you want in this case is the enumerate function as all you're after is the index of the current iteration:

# loop through the list of geodataframes
for index, gdf in enumerate(geo_chunks):
    print(index)
    <do more stuff>
1
  • Worked beautifully. Thank you for the clear explanation. I did not realize list.index() worked that way.
    – Kristen G.
    Commented Jun 8, 2021 at 13:42

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.