John Constantine
John Constantine

Reputation: 1092

Data Wrangling in Python

I have a dataframe df in python

Age     product
------------------
21          apple
11          orange
eighteen    mango
35          pineapple
35          122
NA          apple
30          -1

I only want numeric columns in age, how would I drop the rows which are not integers.

Similarly in product, I need only strings, how would I drop the values which are not strings.

Upvotes: 0

Views: 177

Answers (1)

akuiper
akuiper

Reputation: 215127

A fairly safe way to check numeric values is use pd.isnumeric(..., errors='coerce') and then check nulls; Since pandas can contain different data types in a single column, str.isnumeric returns NaN if the value is actual numeric type and it doesn't recognize negative numbers as numeric because python doesn't:

isnumeric = lambda s: pd.to_numeric(s, errors='coerce').notnull()
df[isnumeric(df['Age']) & ~isnumeric(df['product'])]

#  Age    product
#1  21      apple
#2  11     orange
#4  35  pineapple

This method only checks numeric values, you'll need extra logic if you need to check integers.

Upvotes: 3

Related Questions