输入时df.dtypes,我们有类型列表.但是,是否有一种简单的方法来获得输出
{'col1': np.float32, ...}
Run Code Online (Sandbox Code Playgroud)
或者我需要自己编写一个函数?
ayh*_*han 17
类型返回对象df.dtypes是pandas.Series.它有一个to_dict方法:
df = pd.DataFrame({'A': [1, 2],
'B': [1., 2.],
'C': ['a', 'b'],
'D': [True, False]})
df
Out:
A B C D
0 1 1.0 a True
1 2 2.0 b False
df.dtypes
Out:
A int64
B float64
C object
D bool
dtype: object
df.dtypes.to_dict()
Out:
{'A': dtype('int64'),
'B': dtype('float64'),
'C': dtype('O'),
'D': dtype('bool')}
Run Code Online (Sandbox Code Playgroud)
字典中的值来自dtype类.如果您希望将名称作为字符串,则可以使用apply:
df.dtypes.apply(lambda x: x.name).to_dict()
Out: {'A': 'int64', 'B': 'float64', 'C': 'object', 'D': 'bool'}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4095 次 |
| 最近记录: |