2

I have a pandas dataframe df which looks like this

enter image description here

df = {'Regions': {0: 'REGION1', 1: 'REGION2'}, 'x': {0: '1249-43,1269-12,1280-12', 1: '1267-12,1269-12,1280-12'}}

and a list of raster files called rasters

rasters = 'SI_1206-33_50cm.tif', 'SI_1249-43_50cm.tif', 'SI_1269-12_50cm.tif', 'SI_1267-12_50cm.tif', 'SI_3865-17_50cm.tif' 

What I'd like to do is to create a new list of all entries in rasters that match the strings in df.x per region.

for index, row in df.iterrows():
    region = row['Regions']
    tiffs = row['x']
    rasterlist = []
    for raster in rasters:
        if raster in tiffs:
            rasterlist = rasterlist.append(raster)
            print(rasterlist)

As a result when iterating over rasters, I am attempting to get for the first iteration a rasterlist containing 'SI_1249-43_50cm.tif' and 'SI_1269-12_50cm.tif' for REGION1 and for the second iteration a rasterlist containing only 'SI_1267-12_50cm.tif' for REGION2. The list rasterlist I would like to use as input for further processing using the MosaicToNewRaster_management fucntion in arcpy.

What does not seem to work with this code is the pattern matching, I get an empty rasterlist variable for each iteration. I assume this is the case because the different list items in df.x are separated by a comma and the in function does not seem to work. This is where I am stuck and hoping to get some input.

2
  • Can you provide the output of this or specify what about this doesn't work for you? Commented Dec 19, 2018 at 14:35
  • @NickChapman I just edited the question and tried to explain (hopefully clear enough?) what does not work for me. Commented Dec 19, 2018 at 14:44

1 Answer 1

3

You are checking in the wrong direction. You are using

if raster in tiffs

but tiffs is just something like '1249-43,1269-12,1280-12' which obviously none of the rasters are in. You need to split apart the list of tiffs and check in the reverse direction to see if any of the tiffs are in the rasters.

tiffs = row['x'].split(',')
raster_list = []
for raster in rasters:
    for tiff in tiffs:
        if tiff in raster:
            raster_list.append(raster)
            # Assuming each tiff matches with only one raster
            # you can break to save some time here.
            break
print(raster_list)

If you could tell us more about the mapping between the rasters and tiffs then there's probably something more efficient that can be done using dicts or sets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.