选择所有浮点类型的所有列

时间:2018-03-25 12:10:59

标签: python pandas numpy floating-point

我的数据框中有两列都是float32和float64类型的列,我想选择这两种类型但是使用

categorical = (df_.dtypes.values != np.dtype('float')) or

categorical = (df_.dtypes.values != np.dtype(float)) or 

categorical = (df_.dtypes.values != np.dtype('float_'))

不起作用

如何引用这两种类型的花车?

2 个答案:

答案 0 :(得分:2)

您可以使用基类np.floating来选择所有np.float* dtypes:

In [84]: df
Out[84]:
          a         b         c  d      e
0  0.412986  0.610356  0.413086  0  False
1  0.829305  0.776647  0.829102  1  False
2  0.748733  0.615909  0.748535  2   True
3  0.379850  0.529901  0.379883  3  False
4  0.939924  0.163925  0.939941  4  False

In [85]: df.dtypes
Out[85]:
a    float64
b    float32
c    float16
d      int32
e       bool
dtype: object

In [86]: df.select_dtypes(include=[np.floating])
Out[86]:
          a         b         c
0  0.412986  0.610356  0.413086
1  0.829305  0.776647  0.829102
2  0.748733  0.615909  0.748535
3  0.379850  0.529901  0.379883
4  0.939924  0.163925  0.939941

或使用字符串floating

In [87]: df.select_dtypes(include=['floating'])
Out[87]:
          a         b         c
0  0.412986  0.610356  0.413086
1  0.829305  0.776647  0.829102
2  0.748733  0.615909  0.748535
3  0.379850  0.529901  0.379883
4  0.939924  0.163925  0.939941

答案 1 :(得分:1)

我认为需要select_dtypes同时指定float s:

df.select_dtypes(include=[np.float64, np.float32])

<强>示例

df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})
df['B'] = df['B'].astype(np.float64)
df['C'] = df['C'].astype(np.float32)
df['D'] = df['D'].astype('float')
print (df.dtypes)
A     object
B    float64
C    float32
D    float64
E      int64
F     object
dtype: object

print (df.select_dtypes(include=[np.float64, np.float32]))
     B    C    D
0  4.0  7.0  1.0
1  5.0  8.0  3.0
2  4.0  9.0  5.0
3  5.0  4.0  7.0
4  5.0  2.0  1.0
5  4.0  3.0  0.0

默认float仅选择float64

print (df.select_dtypes(include=['float']))
     B    D
0  4.0  1.0
1  5.0  3.0
2  4.0  5.0
3  5.0  7.0
4  5.0  1.0
5  4.0  0.0