0

I have a numpy array of strings of numbers that I am trying to convert to floats, but some of the numbers have stars around them. Is there any way to remove this without changing the actual number or its position (index) in the numpy arrray? Code below:

nums = np.array(['57', '98.8', '*32.56*', '4.36', '*654.56*', '89.21'])

Ideal output would be:

nums = np.array(['57', '98.8', '32.56', '4.36', '654.56', '89.21'])

After which I would be able to convert it into an array of floats instead of an array of strings

2 Answers 2

1

you can do it without a loop using np.char

import numpy as np

nums = np.array(['57', '98.8', '*32.56*', '4.36', '*654.56*', '89.21'])
nums_float = np.char.strip(nums,'*').astype(float)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this

nums = np.array([item.strip("*") for item in nums])

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.