Ион Сынкетру Ион Сынкетру - 1 year ago 101
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):
number = df.isreal()
except ValueError:
number = 'Unknown'

map(age, df_clean)

Answer Source
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')

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download