0

I am reading various image files from a folder iteratively in a for loop. After each image is being read, the data in the image is goign through manipulations and at the end of for loop, I have a numpy array.

I am looking for a way to append the numpy array into a pandas data frame, as columns. Meaning, if I have five image files --> five numpy arrays --> five columns in the data frame.

I tried by converting it into list and then into dataframe (as follows)

for f in glob.glob(path + "/*.tif"):
    #various manipulations on images.....

    # A numpy array - Example numpy array output
    a = np.array([1 2 3 4 5])
    a_list = a.tolist()
    lis.append(a_list)

df= pd.concat(lis, axis=1, ignore_index=True)

This process did not work and it is also inefficient.

Looking for suggestions on a simple way to add the numpy array that comes out of the for loop to a pandas data frame. At the end each numpy array output from the loop, gets added as a column.

Jesh Kundem
  • 960
  • 3
  • 18
  • 30
  • Could you generate column names in your loop and then assign each numpy array as a new column with `df[colname] = a`? – Peter Leimbigler Sep 15 '21 at 18:58
  • My column names are file names. I am using glob, so not sure on how to get the file names in a glob for loop - individually – Jesh Kundem Sep 15 '21 at 19:01
  • Based on https://stackoverflow.com/questions/7336096/python-glob-without-the-whole-path-only-the-filename, you could add `import os` to the top of your file, then add inside your loop: `filename = os.path.basename(f)`, and finally `df[filename] = a` – Peter Leimbigler Sep 15 '21 at 19:10
  • That works. Thanks – Jesh Kundem Sep 15 '21 at 19:15

0 Answers0