Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas select only numeric or integer field from dataframe

I have this Pandas dataframe (df):

     A    B
0    1    green
1    2    red
2    s    blue
3    3    yellow
4    b    black

A type is object.

I'd select the record where A value are integer or numeric to have:

     A    B
0    1    green
1    2    red
3    3    yellow

Thanks

like image 973
franco_b Avatar asked Jul 22 '14 09:07

franco_b


People also ask

How do you only get numeric columns?

We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric columns.

How do you check if a column has numeric values in pandas?

Pandas str. isdigit() method is used to check if all characters in each string in series are digits. Whitespace or any other character occurrence in the string would return false. If the number is in decimal, then also false will be returned since this is a string method and '.


1 Answers

Call apply on the dataframe (note the double square brackets df[['A']] rather than df['A']) and call the string method isdigit(), we then set param axis=1 to apply the lambda function row-wise. What happens here is that the index is used to create a boolean mask.

In [66]:
df[df[['A']].apply(lambda x: x[0].isdigit(), axis=1)]
Out[66]:
       A       B
Index           
0      1   green
1      2     red
3      3  yellow

Update

If you're using a version 0.16.0 or newer then the following will also work:

In [6]:
df[df['A'].astype(str).str.isdigit()]

Out[6]:
   A       B
0  1   green
1  2     red
3  3  yellow

Here we cast the Series to str using astype and then call the vectorised str.isdigit

Also note that convert_objects is deprecated and one should use to_numeric for the latest versions 0.17.0 or newer

like image 60
EdChum Avatar answered Sep 30 '22 13:09

EdChum