I am trying to extract only numeric values from all the columns in a list, whether it is on the right, left or middle of any characters.
I have a dataframe that looks like the below:
df = pd.DataFrame({
'A': ['1', 3, "1", "cad -2", 3, 4.876, np.nan],
'B': ['116', 'CAD -2.6399', 'CAD -3', '4 $', '$5%', 'A', '-1.2 2']
})
df
I tried the below code but it is removing - from column "A" row 4 and column "B" row 3
l = ["A", "B"]
for columns in l:
if isinstance(df[columns], object):
df[columns] = df[columns].astype('str').str.extract("([-+]?\d*\.\d+|\d+)").astype(float)
df
I want my final dataframe to look like below:
A B
1 116
3 -2.6399
1 -3
-2 4
3 5
4.876 NaN
NaN -1.2