我有具有object,int64,float64
数据类型的pandas数据框。我想获取列的列名int64 and float64
。我在熊猫中使用以下命令,但似乎不起作用
cat_num_prv_app = [num for num in list(df.columns) if isinstance(num, (np.int64,np.float64))]
Run Code Online (Sandbox Code Playgroud)
以下是我的数据类型
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1670214 entries, 0 to 1670213
Data columns (total 37 columns):
ID 1670214 non-null int64
NAME 1670214 non-null object
ANNUITY 1297979 non-null float64
AMOUNT 1670214 non-null float64
CREDIT 1670213 non-null float64
Run Code Online (Sandbox Code Playgroud)
我想将列名存储ID,ANNUITY,AMOUNT and CREDIT
在变量中,以后可以用它来对数据框进行子集化。
使用select_dtypes
与np.number
用于选择所有数字列:
df = pd.DataFrame({'A':list('abcdef'),
'B':[4.5,5,4,5,5,4],
'C':[7.4,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':list('aaabbb')})
print (df)
A B C D E
0 a 4.5 7.4 1 a
1 b 5.0 8.0 3 a
2 c 4.0 9.0 5 a
3 d 5.0 4.0 7 b
4 e 5.0 2.0 1 b
5 f 4.0 3.0 0 b
print (df.dtypes)
A object
B float64
C float64
D int64
E object
dtype: object
cols = df.select_dtypes([np.number]).columns
print (cols)
Index(['B', 'C', 'D'], dtype='object')
Run Code Online (Sandbox Code Playgroud)
这里可以指定float64
和int64
:
df = pd.DataFrame({'A':list('abcdef'),
'B':[4.5,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':list('aaabbb')})
df['D'] = df['D'].astype(np.int32)
print (df.dtypes)
A object
B float64
C int64
D int32
E object
dtype: object
cols = df.select_dtypes([np.int64,np.float64]).columns
print (cols)
Index(['B', 'C'], dtype='object')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3910 次 |
最近记录: |