简体   繁体   English

如何从熊猫数据框中的列中删除非数字字符?

[英]How to remove non-numeric characters from a column in a pandas dataframe?

p_dataset p_dataset

I want to remove the "Rs."我想删除“卢比”。 and commas and just get the numeric values of the column.和逗号,只获取列的数值。 I tried the following code but I'm not getting the output.我尝试了以下代码,但没有得到输出。

def remove_charaters(value):
numbers = []
for word in value.split():
    if word.isdigit() or word=='.': #or condition for decimal point
        numbers.append(int(word))
num=''.join(numbers)
print(num)
return (float(num))

cm=[]
for i in p_dataset['City_Mileage'].astype(str):
    cm.append(remove_charaters(i))
print(cm)
p_dataset['City_Mileage']=cm

EDIT: nevermind the previous answer.编辑:不要介意以前的答案。 I just saw the sample data我刚刚看到示例数据

You can simply use你可以简单地使用

p_dataset['City_Mileage'].str.replace("Rs. ", '').astype(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM