Skip to content Skip to sidebar Skip to footer

Skipping Char-string Values While Comparing To A Column Values Of Mixed Type To Int Or Float In Pandas Dataframe

I have a dataframe in which one column have mixed type values in it: df name ref a 100 b 103.78 c own d 108 e abc@yahoo.com f 110.45 So the ref col

Solution 1:

I think you can use to_numeric and notnull:

printdf[(pd.to_numeric(df['ref'], errors='coerce').notnull()) & (df['ref']>103)]
  name     ref
1    b  103.78
3    d  108.00
5    f  110.45

Solution 2:

Coerce df.ref to numeric values, use gt to get a boolean mask of those greater than zero, and display the original uncoerced values.

df = pd.DataFrame({'name': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'},
                   'ref': {0: 100, 1: 103.78, 2: 'own', 3: 108, 4: 'abc@yahoo.com', 5: 110.45}})

>>> df[pd.to_numeric(df.ref, 'coerce').gt(103)]
  name     ref
1    b  103.78
3    d     108
5    f  110.45

Solution 3:

This returns a boolean series that can be used as a mask, obtaining all df rows in which ref can be converted to numeric.

pd.to_numeric(df.ref,'coerce').notnull()

This is not enough as the column dtype is still str.

df[pd.to_numeric(df.ref,'coerce').notnull()].ref > 105

So you have to use astype(int) before making the comparison.

df[pd.to_numeric(df.ref,'coerce').notnull()].ref.astype(int) > 105

And this will finally return the mask that you want. So this should work and will not modify your string values:

d = df[df[pd.to_numeric(df.ref,'coerce').notnull()].ref.astype(int) > 105]

Post a Comment for "Skipping Char-string Values While Comparing To A Column Values Of Mixed Type To Int Or Float In Pandas Dataframe"