PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

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

 July 27, 2022     crop, deep-learning, image-processing, python, scikit-image     No comments   

Issue

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!



Answered By - Juan
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing