Ион Сынкетру Ион Сынкетру - 1 month ago 14
Python Question

Looking for numbers with letters

I want to select a DataFrame column, iterate over it and select only the the numbers, and to replace the numbers that contains letters and other sings with 'Unknow'. I've tried isreal() method but it didn't work. Is there a way to acomplish this task without a function?

%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)

Answer
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