1

I have a numpy array gven below:

array([[' 0.12E'],
       [' 0.00F'],
       [' 0.00F'],
       ..., 
       [' 0.00G'],
       [' 0.00G'],
       [' 0.00G']], dtype=object)

i need to remove that E F and G and covert each element in the array to Float. How do i do it??

2 Answers 2

4

Option 1
Use numpy string types to automatically trim the last character. Then convert to float
If you can trust that all entries are 6 characters long and end in a character that needs to be trimmed:...

a.astype('<U5').astype(float)

array([[ 0.12],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ]])

Option 2
Use pd.DataFrame.replace with regex=True to remove non-digits, then convert to float.

pd.DataFrame(a).replace('[^\d\.]', '', regex=True).astype(float).values

array([[ 0.12],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ],
       [ 0.  ]])
Sign up to request clarification or add additional context in comments.

Comments

1

Use:

s = pd.Series(a[:, 0])
a = s.str.strip().str[:-1].astype(float).values.reshape(-1, 1)
print (a)
[[ 0.12]
 [ 0.  ]
 [ 0.  ]
 [ 0.  ]
 [ 0.  ]
 [ 0.  ]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.