Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop float values from a column - pandas

I have a dataframe given shown below

df = pd.DataFrame({
 'subject_id':[1,1,1,1,1,1],
  'val' :[5,6.4,5.4,6,6,6]
 })

It looks like as shown below

enter image description here

I would like to drop the values from val column which ends with .[1-9]. Basically I would like to retain values like 5.0,6.0 and drop values like 5.4,6.4 etc

Though I tried below, it isn't accurate

df['val'] = df['val'].astype(int)
df.drop_duplicates()  # it doesn't give expected output and not accurate.

I expect my output to be like as shown below

enter image description here

like image 237
The Great Avatar asked Jan 25 '23 21:01

The Great


1 Answers

First idea is compare original value with casted column to integer, also assign integers back for expected output (integers in column):

s = df['val']
df['val'] = df['val'].astype(int)

df = df[df['val'] == s]
print (df)
   subject_id  val
0           1    5
3           1    6
4           1    6
5           1    6

Another idea is test is_integer:

mask = df['val'].apply(lambda x: x.is_integer())
df['val'] = df['val'].astype(int)

df = df[mask]
print (df)

   subject_id  val
0           1    5
3           1    6
4           1    6
5           1    6

If need floats in output you can use:

df1 = df[ df['val'].astype(int) == df['val']]
print (df1)
   subject_id  val
0           1  5.0
3           1  6.0
4           1  6.0
5           1  6.0
like image 72
jezrael Avatar answered Jan 30 '23 00:01

jezrael