2017-05-10 41 views
3

我需要在包含在列名仅数值熊猫选择列,例如找到数字列名:在熊猫

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 

,所以我需要只选择前五个栏。喜欢的东西:

df[df.columns.isnumeric()] 

编辑

我想出了解决方案:

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

不是很符合Python或pandasian,但它的作品。

+1

尝试'df._get_numeric_data()' – gobrewers14

+0

@ gobrewers14,试过了,也给出'ids'列,这是不需要的。 –

+2

那么你的问题还不清楚。 “我需要选择Pandas中只包含数字值的列。” 'ids'是数字。 – gobrewers14

回答

5

尝试

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  

编辑: 如果你有兴趣的选择是数字列名,这里的东西,你可以做。

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()] 

你得到

0.0 1.0 2.0 
0 1 3 7 
1 2 4 8 
+0

谢谢,但它也选择名称为'ids'的最后一列,它不应该。 –

+1

@ArnoldKlein,那么你应该改写(或更好地开一个新的)问题。这个答案完美地回答你的问题 - 这是选择__all__数字列的最习惯方式 – MaxU

+1

不包含id的唯一方法是将id的dtype更改为object。 Pl看编辑 – Vaishali

1

下面是编辑部分答案:

我已经特意创建了一个可以被转换为数字的列名实数和字符串的混合物:

In [44]: df.columns.tolist() 
Out[44]: [0, 1, 2, 3, '4', 'window_label', 'next_states', 'ids'] 
# NOTE:    ^

我们可以使用pd.to_numeric(..., errors='coerce')方法:

In [41]: df.columns[pd.to_numeric(df.columns, errors='coerce').to_series().notnull()] 
Out[41]: Index([0, 1, 2, 3, '4'], dtype='object') 

In [42]: cols = df.columns[pd.to_numeric(df.columns, errors='coerce').to_series().notnull()] 

In [43]: df[cols] 
Out[43]: 
     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 
+0

太好了,谢谢!让我试试它是如何工作的。 –

1

我发现another question在这个网站是非常相关的。我使用了该代码并将其应用于您的问题。我还在列名中添加了一个浮点数,以确保它与intfloat一起工作。它看起来像:

import pandas as pd 

df = pd.DataFrame({0: [17.0, 18, 16, 15, 15], 
        1: [18.0, 16, 15, 15, 16], 
        2.0: [16.0, 15, 15, 16, 15], 
        3: [15.0, 15, 16, 15, 17], 
        4: [15.0, 16, 15, 17, None], 
        'window_label': ['ddddd' for i in range(5)], 
        'next_states': ['d' for i in range(5)], 
        'ids': [13.0 for i in range(5)]}) 

num_cols = [] 
for col in df.columns.values: 
    try: 
     float(col) 
     num_cols.append(col) 
    except ValueError: 
     pass 

print(df[num_cols]) 

,结果是这样的:

 0  1 2.0  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 

EDIT1:我只是意识到,你可以保持数字确定在发电机的功能,并有一个稍快/肯定少了内存密集型做同样事情的方法。

import pandas as pd 


def is_num(cols): 
    for col in cols: 
     try: 
      float(col) 
      yield col 
     except ValueError: 
      continue 

df = pd.DataFrame({0: [17.0, 18, 16, 15, 15], 
        1: [18.0, 16, 15, 15, 16], 
        2.0: [16.0, 15, 15, 16, 15], 
        3: [15.0, 15, 16, 15, 17], 
        4: [15.0, 16, 15, 17, None], 
        'window_label': ['ddddd' for i in range(5)], 
        'next_states': ['d' for i in range(5)], 
        'ids': [13.0 for i in range(5)]}) 

print(df[[col for col in is_num(df.columns.values)]]) 

产生与上述完全相同的结果,虽然它的可读性稍差。

0

如果你只是在寻找数字的列名,我认为这应该工作:

df.columns[df.columns.str.isnumeric()] 

或本

df.iloc[:,df.columns.str.isnumeric()]