0

I have a dataframe and I want to remove rows containing non-numeric values and also convert float to int

# Dataframe
productid
7819210
9600146.0
031351AA2
3255214
0018392MM
1785506
6924986
1915444
6007828.0

# cuurent approach
df.productid = df[df.productid.apply(lambda x: str(x).isnumeric())].astype(int) # It also remove floating point values because they contain decimal which is non-numeric 

I also made a method which does this

def filternumeric(x):
    if (re.search('\d+', str(x))): pass
    else: return x

df.productid = df[df.productid.apply(filternumeric)].astype(int)

But it is also not working correctly. Any better suggestions ?

4
  • It contains df.applymap which I think works on the whole dataframe but I want to work with only one column Commented Aug 5, 2017 at 17:08
  • 2
    Please see the linked answers on that link as well. These question has been answered multiple times here. Commented Aug 5, 2017 at 17:10
  • 1
    Try df[pd.to_numeric(df.productid, errors='coerce').notnull()] Commented Aug 5, 2017 at 17:10
  • And, stackoverflow.com/q/33961028 Commented Aug 5, 2017 at 17:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.