Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find numeric column names in Pandas

I need to select columns in Pandas which contain only numeric values in column names, for example:

df=
          0     1     2     3     4 window_label next_states       ids
0      17.0  18.0  16.0  15.0  15.0        ddddd           d      13.0
1      18.0  16.0  15.0  15.0  16.0        ddddd           d      13.0
2      16.0  15.0  15.0  16.0  15.0        ddddd           d      13.0
3      15.0  15.0  16.0  15.0  17.0        ddddd           d      13.0
4      15.0  16.0  15.0  17.0   NaN        ddddd           d      13.0

so I need to select only first five columns. Something like:

df[df.columns.isnumeric()]

EDIT

I came up with the solution:

digit_column_names = [num for num in list(df.columns) if isinstance(num, (int,float))]
df_new = df[digit_column_names]

not very pythonic or pandasian, but it works.

like image 759
Arnold Klein Avatar asked May 10 '17 16:05

Arnold Klein


2 Answers

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
like image 52
Vaishali Avatar answered Oct 04 '22 16:10

Vaishali


How about this solution?
This checks if every character of the column is a digit.

cols = [col for col in df.columns if all(char.isdigit() for char in col)]
df[cols]
like image 28
igorkf Avatar answered Oct 04 '22 17:10

igorkf