How to Find Numeric Columns in Pandas

how to get numeric column names in pandas dataframe

Use select_dtypes with np.number for select all numeric columns:

df = pd.DataFrame({'A':list('abcdef'),
'B':[4.5,5,4,5,5,4],
'C':[7.4,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':list('aaabbb')})

print (df)
A B C D E
0 a 4.5 7.4 1 a
1 b 5.0 8.0 3 a
2 c 4.0 9.0 5 a
3 d 5.0 4.0 7 b
4 e 5.0 2.0 1 b
5 f 4.0 3.0 0 b

print (df.dtypes)
A object
B float64
C float64
D int64
E object
dtype: object

cols = df.select_dtypes([np.number]).columns
print (cols)
Index(['B', 'C', 'D'], dtype='object')

Here is possible specify float64 and int64:

df = pd.DataFrame({'A':list('abcdef'),
'B':[4.5,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':list('aaabbb')})

df['D'] = df['D'].astype(np.int32)
print (df.dtypes)
A object
B float64
C int64
D int32
E object
dtype: object

cols = df.select_dtypes([np.int64,np.float64]).columns
print (cols)
Index(['B', 'C'], dtype='object')

Identify pandas dataframe columns containing both numeric and string

You can def your own function

def findchrandnum(x):
try :
return all(x.str.isalnum() & ~x.str.isalpha() & ~x.str.isdigit())
except:
return False
df.apply(findchrandnum)
Out[66]:
ltv False
age False
job False
UniqueID True
dtype: bool

Pandas way to do arithmetic operations on only numeric columns

You can find the numeric columns using select_dtypes:

s = df.select_dtypes("number").columns
df[s] *= 100

print (df)

name1 val1 name2 val21 val22
0 a 100 aa 100 500
1 b 200 bb 200 600
2 c 300 cc 300 700

How to check if a pandas dataframe contains only numeric values column-wise?

You can check that using to_numeric and coercing errors:

pd.to_numeric(df['column'], errors='coerce').notnull().all()

For all columns, you can iterate through columns or just use apply

df.apply(lambda s: pd.to_numeric(s, errors='coerce').notnull().all())

E.g.

df = pd.DataFrame({'col' : [1,2, 10, np.nan, 'a'], 
'col2': ['a', 10, 30, 40 ,50],
'col3': [1,2,3,4,5.0]})

Outputs

col     False
col2 False
col3 True
dtype: bool

How to get only numeric type columns from dataframe

The following works

import numpy as np
import pandas as pd

df = pd.DataFrame({'float_col': [1.0],
'int_col': [1],
'datetime_col': [pd.Timestamp('20180310')],
'string_col': ['foo']})
df_int = df[df.columns[df.dtypes == np.int64]]

In this case, df is the data frame

   float_col  int_col datetime_col string_col
0 1.0 1 2018-03-10 foo

and df_int is the data frame

   int_col
0 1

Python Pandas: How to find in dataframe object type columns which has numeric data?

Not straight forward, the following is a wilcard and is all weather though

First select dtypes='object'
Second attempt to coerce them to numeric, setting errors='coerce', what that will do is if alphanumeric, it will output them as NaN giving you the privilege to leverage dropna() and remain with only numeric/object dtypes

Code below

 df.select_dtypes('object').apply(lambda x: pd.to_numeric(x,errors='coerce')).dropna(axis=1)

Outcome

    B
0 29.85
1 85.43
2 33.87

find numeric column names in Pandas

Try

df.ids = df.ids.astype('object')    
new_df = df.select_dtypes([np.number])

0 1 2 3 4
0 17.0 18.0 16.0 15.0 15.0
1 18.0 16.0 15.0 15.0 16.0
2 16.0 15.0 15.0 16.0 15.0
3 15.0 15.0 16.0 15.0 17.0
4 15.0 16.0 15.0 17.0 NaN

EDIT:
If you are interested in selecting column names that are numeric, here is something that you can do.

df = pd.DataFrame({0: [1,2], '1': [3,4], 'blah': [5,6], 2: [7,8]})
df.columns = pd.to_numeric(df.columns, errors = 'coerce')
df[df.columns.dropna()]

You get

    0.0 1.0 2.0
0 1 3 7
1 2 4 8


Related Topics



Leave a reply



Submit