Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe strip non-numeric characters

Tags:

python

pandas

I'm working with data in the following form:

Accuracy 26.15%, error rate 0.00%, not classified 73.85%
Accuracy 29.68%, error rate 0.00%, not classified 70.32%
Accuracy 33.98%, error rate 0.00%, not classified 66.02%
Accuracy 35.34%, error rate 0.00%, not classified 64.66%
Accuracy 35.75%, error rate 0.00%, not classified 64.25%
Accuracy 37.51%, error rate 0.00%, not classified 62.49%
Accuracy 38.63%, error rate 0.00%, not classified 61.37%
Accuracy 40.81%, error rate 0.00%, not classified 59.19%
Accuracy 41.22%, error rate 0.00%, not classified 58.78%
Accuracy 41.99%, error rate 0.00%, not classified 58.01%
Accuracy 42.34%, error rate 0.00%, not classified 57.66%
Accuracy 42.40%, error rate 0.00%, not classified 57.60%
Accuracy 43.05%, error rate 0.00%, not classified 56.95%
Accuracy 44.29%, error rate 0.00%, not classified 55.71%
Accuracy 44.35%, error rate 0.00%, not classified 55.65%
Accuracy 44.76%, error rate 0.00%, not classified 55.24%
Accuracy 45.29%, error rate 0.00%, not classified 54.71%
Accuracy 45.35%, error rate 0.00%, not classified 54.65%
Accuracy 95.35%, error rate 4.24%, not classified 0.41%
Accuracy 95.76%, error rate 4.24%, not classified 0.00%
Stats on test data
Accuracy 94.74%, error rate 5.26%, not classified 0.00%

How can I load this in to a pandas dataframe, with the headings 'Accuracy', 'Error rate', and 'Not classified', whilst also removing non-numeric characters from the data fields.

So far I have:

pd.read_csv("test.csv", names=['Accuracy', 'Error rate', 'Not classified'])

But this produces:

    Accuracy    Error rate  Not classified
0   Accuracy 25.85% error rate 0.00%    not classified 74.15%
1   Accuracy 29.92% error rate 0.00%    not classified 70.08%
2   Accuracy 33.69% error rate 0.00%    not classified 66.31%
3   Accuracy 36.16% error rate 0.00%    not classified 63.84%
4   Accuracy 37.16% error rate 0.00%    not classified 62.84%
5   Accuracy 39.28% error rate 0.00%    not classified 60.72%
6   Accuracy 39.58% error rate 0.00%    not classified 60.42%
7   Accuracy 40.05% error rate 0.00%    not classified 59.95%
like image 389
HenryPaul Avatar asked Mar 04 '23 22:03

HenryPaul


2 Answers

You can do it using pandas.DataFrame.replace():

df.replace(r'[a-zA-Z%]', '', regex=True, inplace=True)

If your ultimate goal is to convert those values to numbers perform

df.apply(pd.to_numeric)

or do it column by column

df['Accuracy'] = pd.to_numeric(df['Accuracy']) # and so on
like image 173
ayorgo Avatar answered Mar 29 '23 21:03

ayorgo


You can do this way using str.replace(r"[a-zA-Z]",'') to remove the alphabet characters. If you need you can add more characters on this class to remove those also.

import pandas as pd

df = pd.read_csv("test.csv", names=['Accuracy', 'Error rate', 'Not classified'])

df['Accuracy'] = df['Error rate'].str.replace(r"[a-zA-Z]",'')
df['Error rate'] = df['Error rate'].str.replace(r"[a-zA-Z]",'')
df['Not classified'] = df['Not classified'].str.replace(r"[a-zA-Z]",'')
print(df)

DEMO: https://repl.it/@SanyAhmed/EarnestTatteredRepo

like image 25
Always Sunny Avatar answered Mar 29 '23 20:03

Always Sunny