I have a dataframe df
like the follwoing
Rooms BFS
0 3.5 4201
1 1.5 4201
2 NA 4201
3 NA 4201
4 5.5 4201
5 5 4201
6 4.5 4201
7 3 4201
8 4201
9 3 4201
I want to drop
all the non numeric values from the column Rooms
. All the values in Rooms
are now considered as str
Use to_numeric
with errors='coerce'
and Series.notna
for filtering by boolean indexing
:
df = df[pd.to_numeric(df['Rooms'],errors='coerce').notna()]
print (df)
Rooms BFS
0 3.5 4201
1 1.5 4201
4 5.5 4201
5 5 4201
6 4.5 4201
7 3 4201
9 3 4201
If need numeric in output first assign to same column and then use DataFrame.dropna
:
df['Rooms'] = pd.to_numeric(df['Rooms'],errors='coerce')
df = df.dropna(subset=['Rooms'])
print (df)
Rooms BFS
0 3.5 4201
1 1.5 4201
4 5.5 4201
5 5.0 4201
6 4.5 4201
7 3.0 4201
9 3.0 4201
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.