I have a pandas dataframe in which a column must accept only decimal fields, what can I do to skip or delete all of the rows that values in this columns are strings?
Asked
Active
Viewed 570 times
-1
-
Welcome to Stack Overflow. Please read how to ask good [questions](https://stackoverflow.com/help/how-to-ask). Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) 3. Error Message (preferably full Traceback to help others review and provide feedback). Sometimes the same question may have already been asked. Make sure your question is not a [duplicate](https://stackoverflow.com/help/duplicates) – Joe Ferndz Feb 08 '21 at 23:55
-
2Have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and show some code for what you've already tried, and what went wrong with your attempts, so that we can better understand how to help – G. Anderson Feb 08 '21 at 23:56
-
Can you please share your sample dataframe, expected output dataframe, and as per my prev comment, your MRE. – Joe Ferndz Feb 08 '21 at 23:56
1 Answers
0
You can convert the column to a numeric type by using pandas.to_numeric
with errors coerce parameter, which converts all the non-numeric fields to numpy.nan
. Then you can select rows without numpy.nan
at the certain column.
import numpy as np
import pandas as pd
if __name__ == '__main__':
# make a DataFrame
df = pd.DataFrame({'col_str': ['1', '2', '3', '4', '5', "crdtar"]})
df["col_str"] = pd.to_numeric(df["col_str"], errors="coerce")
sel_df = df[~df["col_str"].isnull()]
print(sel_df)
Result:
col_str
0 1.0
1 2.0
2 3.0
3 4.0
4 5.0

abysslover
- 683
- 5
- 14