Python 熊猫+;Numpy:通过数值数据类型过滤DF列会产生意外行为

Python 熊猫+;Numpy:通过数值数据类型过滤DF列会产生意外行为,python,numpy,pandas,Python,Numpy,Pandas,我有下表(示例以方便任何想要尝试的人): 连载;光谱;名称UKST;类风湿关节炎;12月;ra2000;2000年12月;BJG;比塞尔;比古奥尔德;比塞洛德;伽莱克斯;SB_BJ;高级工程师;Zz_helio;奥伯伦;质量;阿贝玛;Z_ABS;KBESTR;R_CRCOR;Z_EMI;NMBEST;信噪比;埃塔型 1.2.TGS436Z001;349;00:11:55.72;-32:32:55.2;00:14:27.05;-32:16:14.6;19.424;19.362;19.430;19.

我有下表(示例以方便任何想要尝试的人):

连载;光谱;名称UKST;类风湿关节炎;12月;ra2000;2000年12月;BJG;比塞尔;比古奥尔德;比塞洛德;伽莱克斯;SB_BJ;高级工程师;Zz_helio;奥伯伦;质量;阿贝玛;Z_ABS;KBESTR;R_CRCOR;Z_EMI;NMBEST;信噪比;埃塔型 1.2.TGS436Z001;349;00:11:55.72;-32:32:55.2;00:14:27.05;-32:16:14.6;19.424;19.362;19.430;19.390;0.062;19.368;18.286;0.2981;0.2981;9月1日;4.1.0.2981;5.4.5700;0.2984;1.3.8;-99.90000 2.1.TGS496Z001;349;00:11:59.29;-33:14:41.3;00:14:30.55;-32:58:00.7;18.842;18.789;18.870;18.840;0.053;18.688;17.291;0.1229;0.1228;10月1日;5.1.0.1229;1.14.3800;-9.9990;0;47.6;-2.58920 3.1.TGS435Z001;349;00:11:49.37;-32:39:57.4;00:14:20.71;-32:23:16.8;18.320;18.265;18.350;18.310;0.055;18.336;17.138;0.1038;0.1038;9月1日;4.1.0.1038;1.9.3800;0.1032;1.28.4;-246500

我们得到的相应的
df
具有以下
dtypes
信息:

>>> df.dtypes
serial        int64
spectra       int64
name         object
UKST          int64
ra           object
dec          object
ra2000       object
dec2000      object
BJG         float64
BJSEL       float64
BJG_OLD     float64
BJSELOLD    float64
GALEXT      float64
SB_BJ       float64
SR_R        float64
z           float64
z_helio     float64
obsrun       object
quality       int64
abemma        int64
Z_ABS       float64
KBESTR        int64
R_CRCOR     float64
Z_EMI       float64
NMBEST        int64
SNR         float64
ETA_TYPE    float64
dtype: object
我想做的只是根据列的数据类型过滤列名称;特别是,我想保留数字列。所以,我想我应该做的就是检查他们的
dtype
是否是
numpy.number

>>> filter(lambda c:df[c].dtypes == numpy.number,df.columns)
['BJG',
'BJSEL',
'BJG_OLD',
'BJSELOLD',
'GALEXT',
'SB_BJ',
'SR_R',
'z',
'z_helio',
'Z_ABS',
'R_CRCOR',
'Z_EMI',
'SNR',
'ETA_TYPE']
但正如我们所看到的,我只得到了
>float
列,而
>int
列被抛在了后面

do通过执行以下操作获得我想要的结果:

>>> filter(lambda c:df[c].dtypes == numpy.floating or df[c].dtypes == numpy.integer, df.columns)
['serial',
'spectra',
'UKST',
'BJG',
'BJSEL',
'BJG_OLD',
'BJSELOLD',
'GALEXT',
'SB_BJ',
'SR_R',
'z',
'z_helio',
'quality',
'abemma',
'Z_ABS',
'KBESTR',
'R_CRCOR',
'Z_EMI',
'NMBEST',
'SNR',
'ETA_TYPE']
(注意:
numpy.floating
numpy.number
在上面的行中给出相同的结果。)

这里的问题是:
numpy.number
不应该“表示”numpy中的任何数字类型(int、float、complex等)? 在阅读了
numpy.core.numerictypes
帮助页面上相应的类层次结构后,我意外地发现了上述行为。。。 有人对此有何评论?我错过什么了吗

干杯。

在列表对象中使用并传递
np.number

In [160]:
df.select_dtypes([np.number]).info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 21 columns):
serial      3 non-null int64
spectra     3 non-null int64
UKST        3 non-null int64
BJG         3 non-null float64
BJSEL       3 non-null float64
BJG_OLD     3 non-null float64
BJSELOLD    3 non-null float64
GALEXT      3 non-null float64
SB_BJ       3 non-null float64
SR_R        3 non-null float64
z           3 non-null float64
z_helio     3 non-null float64
quality     3 non-null int64
abemma      3 non-null int64
Z_ABS       3 non-null float64
KBESTR      3 non-null int64
R_CRCOR     3 non-null float64
Z_EMI       3 non-null float64
NMBEST      3 non-null int64
SNR         3 non-null float64
ETA_TYPE    3 non-null float64
dtypes: float64(14), int64(7)
memory usage: 528.0 bytes
[160]中的

df.选择类型([np.编号]).info()
INT64索引:3个条目,0到2
数据列(共21列):
串行3非空int64
光谱3非空int64
UKST 3非空int64
BJG 3非空浮点64
BJSEL 3非空浮点64
BJG_旧3非空浮点64
BJSELOLD 3非空浮点64
GALEXT 3非空浮点64
SB_BJ 3非空浮点64
SR_R 3非空浮点64
z 3非空浮点64
z_helio 3非零浮点64
质量3非空int64
abemma 3非空int64
Z_ABS 3非空浮点64
KBESTR 3非空int64
R_CRCOR 3非空浮点64
Z_EMI 3非空浮点64
NM3非空int64
信噪比3非零浮点64
ETA_类型3非空浮点64
数据类型:float64(14)、int64(7)
内存使用:528.0字节

does
df.选择类型([np.number])
work?@EdChum:是的。谢谢你的提示。
In [160]:
df.select_dtypes([np.number]).info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 21 columns):
serial      3 non-null int64
spectra     3 non-null int64
UKST        3 non-null int64
BJG         3 non-null float64
BJSEL       3 non-null float64
BJG_OLD     3 non-null float64
BJSELOLD    3 non-null float64
GALEXT      3 non-null float64
SB_BJ       3 non-null float64
SR_R        3 non-null float64
z           3 non-null float64
z_helio     3 non-null float64
quality     3 non-null int64
abemma      3 non-null int64
Z_ABS       3 non-null float64
KBESTR      3 non-null int64
R_CRCOR     3 non-null float64
Z_EMI       3 non-null float64
NMBEST      3 non-null int64
SNR         3 non-null float64
ETA_TYPE    3 non-null float64
dtypes: float64(14), int64(7)
memory usage: 528.0 bytes