Reputation: 63
col A
28
45
67
A
67
C
D
78
89
I want to remove row containing characters(i.e) A, B, C...(can be any from A-Z) I was able to remove A,B,C using the below code
new_df = df[(df['colA'] != 'A') & (df['colA'] != 'B') & (df['colA'] != 'C')]
I feel this is hardcoded as I know the column contains A,B,C. Some other column might contain F,G or something. Any better approach to removing the characters from column.
Note:- colA is of type object in dataframe
Output should be column containing numbers only and datatype should be changed from object to int
Upvotes: 0
Views: 62
Reputation: 323226
Try with isalpha
for no numeric it will return True
then we get the ~
df = df[~df.colA.str.isalpha()]
Out[953]:
colA
0 28
1 45
2 67
4 67
7 78
8 89
Update method 2
df = df[pd.to_numeric(df['col A'],errors='coerce').notnull()]
Out[73]:
colA
0 28
1 45
2 67
4 67
7 78
8 89
Upvotes: 1