12

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    

2 Answers 2

22

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
Sign up to request clarification or add additional context in comments.

1 Comment

Note that to_numeric considers hex literals (0xFF) non-numeric, which may or may not be what you want.
-2

You can use the apply() method along with the isinstance() function. Can replace str with int, float, etc:

df = pd.DataFrame([1,2,4.5,np.NAN,'asdf',5,'string'],columns=['SIC'])
print(df)
      SIC
0       1
1       2
2     4.5
3     NaN
4    asdf
5       5
6  string

print(df[df['SIC'].apply(lambda x: isinstance(x,str))])
      SIC
4    asdf
6  string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.