寻找带字母的数字

时间:2016-10-20 14:59:28

标签: python string pandas int

我想选择一个DataFrame列,迭代它并仅选择数字,并用'Unknow'替换包含字母和其他唱歌的数字。我尝试过isreal()方法,但它没有用。有没有办法在没有功能的情况下完成这项任务?

%matplotlib inline
%pylab inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
file = 'C:/Users/Сынкетру/Desktop/attacks.csv'
df = pd.read_csv(file, sep=',', encoding='ISO-8859-1') 


df_clean = df.Age.dropna()

def age(number):
    try:
        number = df.isreal()
    except ValueError:
        number = 'Unknown'

map(age, df_clean)
print(d)

2 个答案:

答案 0 :(得分:1)

df = pd.DataFrame(dict(A=['1', 2, '_3', '4.', 'hello', 3.14]))

df['A'] = np.where(pd.to_numeric(df.A, 'coerce').notnull(), df.A, 'unknown')
df

enter image description here

答案 1 :(得分:0)

df.Age[~df.Age.apply(np.isreal)] = "unknown"