I'm trying to load shapefiles with years from 1900-2014 into a dictionary in a for loop using geopandas.
I did the same code already except with fiona which worked fine but I am finding at an individual level that the data is more useful for me in geopandas format.
All I did was replace the fiona command with the gpd.read_file. It was running forever so I added the print command and noticed that it simply loops through the files from 1900 to 2014 and starts over again at 1900. If I call any of the items from the dictionary it also appears that they all have the same shapefile stored.
Why is it looping? Why isn't it storing the shapefiles separately in the dictionary?
import geopandas as gpd
import os
import numpy as np
all_data = {}
path = r'C:\Users\'
files = os.listdir(path)
for file in files:
if file.endswith('.shp'):
fname = os.path.join(path, file)
years = np.arange(1900,2015)
for year in years:
shape = gpd.read_file(fname)
print ('Done' str(year))
all_data['data' str(year)] = shape
CodePudding user response:
Every time you move to the next file in the list you re-create your "years" variable and loop through it again. It sounds like you only want this to happen once(?). There is currently nothing in your code that relates the current file name to the current "year", so it will blindly loop through them all. It sounds like you just need to compare the filenames to the current year and check for a match (the break
command can be used to exit loops early, if needed).