熊猫数据框去除非数字字符

时间:2018-12-08 14:25:19

标签: python pandas

我正在使用以下格式的数据:

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%

如何将其加载到熊猫数据框中,标题为“准确性”,“错误率”和“未分类”,同时还要从数据字段中删除非数字字符。

到目前为止,我有:

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

但这会产生:

    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%

2 个答案:

答案 0 :(得分:1)

您可以使用pandas.DataFrame.replace()

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

如果您的最终目标是将这些值转换为数字,则执行

df.apply(pd.to_numeric)

或逐列进行

df['Accuracy'] = pd.to_numeric(df['Accuracy']) # and so on

答案 1 :(得分:1)

您可以使用str.replace(r"[a-zA-Z]",'')删除字母字符。如果需要,可以在此类上添加更多字符以将其删除。

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)

演示: https://repl.it/@SanyAhmed/EarnestTatteredRepo