0

I am doing some image processing in python, and need to crop an area of the image. However, my pixel coordinate data is arranged as three values in one excel column seperated by commas, as follows:

[1345.83,1738,44.26] (i.e. [x,y,r]) - this is exactly how it appears in the excel cell, square brackets and all.

Any idea how I can read this into my script and start cropping images according to the pixel coord values? Is there a function that can seperate them and treat them as three independent values?

Thanks, Rhod

  • 1
    strings have a built-in `split` method that will separate a string into a list based on the passed argument (e.g. `"[1345.83,1738,44.26]".split(",")` gives a list `[1345.83, 1738, 44.26]` – Andrew Mar 17 '20 at 14:47
  • That works when entering the raw values, however how would I then tie it into the workbook? sheet.cell(row=2,column=14).split(",") doesnt work, and neither does sheet["N2"].split(",") – CephaloRhod Mar 17 '20 at 14:55
  • _this is exactly how it appears in the excel cell, square brackets and all._ I haven't use Excel in while, is the whole considered to be a string, or something? What does it look like once you've read it into Python? – AMC Mar 17 '20 at 17:40
  • Feel free to share your Excel spreadsheet via Google Drive, Dropbox or somesuch. – Mark Setchell Mar 17 '20 at 20:04

1 Answers1

1

My understanding is that if you use pandas.read_excel(), you will get a column of strings in this situation. There's lots of options but here I would do, assuming your column name is xyr:

# clean up strings to remove braces on either side
data['xyr_clean'] = data['xyr'].str.lstrip('[').str.rstrip(']')
data[['x', 'y', 'r']] = (
    data['xyr_clean'].str.split(', ', expand=True).astype(float)
)

The key thing to know is that pandas string columns have a .str attribute that contains adapted versions of all or most of Python's built-in string methods. Then you can search for "pandas convert string column to float" to get the last bit!

Juan
  • 5,433
  • 21
  • 23