I'm wondering how to delete rows with decimal points from a column of mixed type in a Pandas data frame.
Suppose I have a column of mixed type (type 'o').
d = {'col1': [1, 2.3, 'Level1']}
test1 = pd.DataFrame(data=d)
test1['col1'].dtypes
dtype('O')
test1
col1
0 1
1 2.3
2 Level1
I will like to delete the row that contains decimal points.
test1
col1
0 1
2 Level1
I tried str.isdecimal() or str.contain('.') didn't work.. Thanks in advance.
CodePudding user response:
This may help:
d = {'col1': [1, 2.3, 'Level1']}
test1 = pd.DataFrame(data=d)
test2 = test1.copy()
for i in range(len(test1)):
if "." in str(test1.iloc[i,0]):
test2.drop(i, axis = 0, inplace = True)
CodePudding user response:
What about using a regex?
m = test1['col1'].astype(str).str.fullmatch('\d \.\d ')
test1[~m]
Or testing the real object type:
m = test1['col1'].apply(lambda x: isinstance(x, float))
test1[~m]
Output:
col1
0 1
2 Level1