jet*_*com 3 python types numpy series pandas
假设我们有一个 pandas 系列,其中包含以下数据类型的混合(字符串、整数和日期时间)
如果我检查 diff_series ( ) 上的 dtype diff_series.dtype
,它会告诉我它是一个预期的对象。我想获得该系列中的不同数据类型。我有以下列表理解,它使我获得了一系列中独特的数据类型。
import datetime
import pandas as pd
>> diff_series = pd.Series(['1','2',3,"random_text",datetime.datetime.now()])
>> set([type(i) for i in diff_series])
set([<type 'str'>, <type 'datetime.datetime'>, <type 'int'>])
Run Code Online (Sandbox Code Playgroud)
但我有一种感觉,应该有一种更有效(潘多拉式)的方式来做到这一点?
我试过
>> diff_series.get_dtype_counts()
object 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
这不是我想要的。有任何想法吗?
我们可以做这样的事情:
In [4]: diff_series.map(type).value_counts()
Out[4]:
<class 'str'> 3
<class 'datetime.datetime'> 1
<class 'int'> 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
或者,不妨“全力以赴”:
In [5]: diff_series.map(type).value_counts().index.values
Out[5]: array([<class 'str'>, <class 'datetime.datetime'>, <class 'int'>], dtype=object)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1364 次 |
最近记录: |