Jsmoka
Jsmoka

Reputation: 59

Drop values with Type(int) in columns

I have two columns (CL1 & CL2):

enter image description here

my goal:

I would drop the values with type Integer from column CL1 with for loop.

what i did:

for Int in df.OriginalName:
    if Int.dtype("int64"):
        df.drop[df.OriginalName].index

Upvotes: 1

Views: 203

Answers (1)

jezrael
jezrael

Reputation: 862611

If there are mixed values - strings, floats and integers is possible test type by isinstance and filter all values without integer type:

df = pd.DataFrame({
    "CL1": ['Hello', 12, 'World', 12.23],
   
})

df = df[~df.CL1.map(lambda x: isinstance(x, int))]
print (df)
     CL1
0  Hello
2  World
3  12.23

But if all values are strings use this trick - convert all values to numeric, replace non numeric to some floats and test if not equal integers values:

df = pd.DataFrame({
    "CL1": ['Hello', '12', 'World', '12.23'],
   
})

s = pd.to_numeric(df.CL1, errors='coerce').fillna(1.1)
df = df[s.ne(s.astype(int))]
print (df)
     CL1
0  Hello
2  World
3  12.23

Upvotes: 2

Related Questions