1

I been using:

df = df.convert_objects(convert_numeric=True).dropna()

but since it's being deprecated I need to replace it with to_numeric

I've tried

df = pd.to_numeric
df = df.dropna()

Sample data:

Name  Race Fav Age  Weight Height Style Cut
John   D    K  23    120   23.5    DD   RET
Rose   Z    U  33    110   47.9    KZ   DEZ
James  Z    U  FF    UK    NOT     Z    R

Would like to convert to drop the rows that are non numeric

Output:

Name  Race Fav Age  Weight Height Style Cut
John   D    K  23    120   23.5    DD   RET
Rose   Z    U  33    110   47.9    KZ   DEZ
magicsword
  • 1,071
  • 2
  • 15
  • 24

3 Answers3

1

I'd do it this way:

In [399]: num_cols = df.columns[df.apply(pd.to_numeric, errors='coerce').any()]

In [400]: df[num_cols] = df[num_cols].apply(pd.to_numeric, errors='coerce')

In [401]: df = df[df.select_dtypes(['number']).notnull().all(1)]

In [402]: df
Out[402]:
   Name Race Fav   Age  Weight  Height Style  Cut
0  John    D   K  23.0   120.0    23.5    DD  RET
1  Rose    Z   U  33.0   110.0    47.9    KZ  DEZ

In [403]: df.dtypes
Out[403]:
Name       object
Race       object
Fav        object
Age       float64
Weight    float64
Height    float64
Style      object
Cut        object
dtype: object
MaxU - stop genocide of UA
  • 191,778
  • 30
  • 340
  • 375
0

You aren't calling to_numeric properly. Since it only applies to columns, if you want to apply it to all columns you'll have to use pd.apply

df = df.apply(pd.to_numeric)
df = df.dropna()
Louise Davies
  • 12,825
  • 5
  • 38
  • 40
0

apply the to_numeric then dropna

df.apply(lambda x :pd.to_numeric(x, errors ='coerce'),axis=1).dropna()
BENY
  • 296,997
  • 19
  • 147
  • 204