Search code examples
pythonimage-processingdeep-learningcropscikit-image

How do I use multiple data points from one Excel column in Python?


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


Solution

  • 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!