Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering string/float/integer values in pandas dataframe columns

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    
like image 732
MMM Avatar asked Jul 26 '17 22:07

MMM


People also ask

How do you filter a DataFrame based on column values?

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.

Can we pass integer in data frame in pandas?

Construction. pandas can represent integer data with possibly missing values using arrays.

Can pandas DataFrame have different data types?

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.


1 Answers

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
like image 148
Scott Boston Avatar answered Oct 13 '22 02:10

Scott Boston