How can I filter only string values/ integer/ float values in one column (SIC
) in a pandas data frame like below?
SIC
1 246804
2 135272
3 898.01
4 3453.33
5 shine
6 add
7 522
8 Nan
9 string
10 29.11
11 20
Using query() to Filter by Column Value in pandas DataFrame. query() function is used to filter rows based on column value in pandas. After applying the expression, it returns a new DataFrame.
Construction. pandas can represent integer data with possibly missing values using arrays.
We can pass any Python, Numpy, or Pandas datatype to change all columns of a Dataframe to that type, or we can pass a dictionary having column names as keys and datatype as values to change the type of selected columns.
You can use the outputs from pd.to_numeric
and boolean indexing.
To get only the strings use:
df[pd.to_numeric(df.SIC, errors='coerce').isnull()]
Output:
SIC
5 shine
6 add
8 Nan
9 string
To get only the numbers use:
df[pd.to_numeric(df.SIC, errors='coerce').notnull()]
Output:
SIC
1 246804
2 135272
3 898.01
4 3453.33
7 522
10 29.11
11 20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With