在Pandas中查找数字列名称

Arn*_*ein 6 python dataframe pandas

我需要在Pandas中选择列中只包含数值的列,例如:

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
Run Code Online (Sandbox Code Playgroud)

所以我只需要选择前五列.就像是:

df[df.columns.isnumeric()]
Run Code Online (Sandbox Code Playgroud)

编辑

我提出了解决方案:

digit_column_names = [num for num in list(df.columns) if isinstance(num, (int,float))]
df_new = df[digit_column_names]
Run Code Online (Sandbox Code Playgroud)

不是非常pythonic或pandasian,但它的工作原理.

Vai*_*ali 8

尝试

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     
Run Code Online (Sandbox Code Playgroud)

编辑:如果您有兴趣选择数字列名,这是您可以做的事情.

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()]
Run Code Online (Sandbox Code Playgroud)

你得到

    0.0 1.0 2.0
0   1   3   7
1   2   4   8
Run Code Online (Sandbox Code Playgroud)