从数据帧中删除特定字符串

2024-04-19 14:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法从数据集中的这些列中清除字符串“km/kg”、“kmpl”、“CC”和“bhp”

下面是示例数据集

Year   | Fuel_Type | Mileage    | Engine  | Power     |
2010   | LPG       | 26.6 km/kg | 998 CC  | 58.16 bhp |
2011   | Diesel    | 19.67 kmpl | 1582 CC | 126.2 bhp |

在这种特殊情况下,请从数据集的引擎、里程和功率列中删除所有字符,以便只保留数字


Tags: 数据字符串示例typeyearengineccpower
2条回答

只需使用df.replaceregex模式进行字符串匹配

df[['Mileage','Engine','Power']] = df[['Mileage','Engine','Power']].replace(to_replace=r'([a-z/]+|[A-Z/]+)', value='', regex=True)

印刷品:

  Year Fuel_Type  Mileage  Engine   Power
0  2010       LPG   26.6    998     58.16 
1  2011    Diesel  19.67    1582    126.2 

您可以尝试使用regular expression来执行此操作

这里有一个关于如何做到这一点的快速示例。另外,我假设您已经知道阅读您右侧的数据集,所以您可以做的就是获取列并对其进行迭代,然后应用我提供的正则表达式示例代码

当涉及到读取数据集时,我个人喜欢使用pandas

import re

l = ["26.6 km/kg","19.67 kmpl","998 CC","58.16 bhp"]


for i in l:
    t = re.sub(r'\D+$','',i)
    print(t)

输出:

26.6
19.67
998
58.16
[Finished in 0.2s]

如果你对\D+$感到好奇,这就是它的意思

\D Returns a match where the string DOES NOT contain digits "\D"

The + sign basically means any occurrences of it 1 or more.

$ means ends with

阅读有关正则表达式here的更多信息

相关问题 更多 >