如何检测 pandas 对象列中的子类型? [英] How could I detect subtypes in pandas object columns?
问题描述
我有下一个DataFrame:
I have the next DataFrame:
df = pd.DataFrame({'a': [100, 3,4], 'b': [20.1, 2.3,45.3], 'c': [datetime.time(23,52), 30,1.00]})
,如果可能的话,我想在没有显式编程循环的情况下检测列中的子类型 .
and I would like to detect subtypes in columns without explicit programming a loop, if possible.
我正在寻找下一个输出:
I am looking for the next output:
column a = [int]
column b = [float]
column c = [datetime.time, int, float]
推荐答案
您应该了解,使用Pandas可以有2种广泛的系列类型:
You should appreciate that with Pandas you can have 2 broad types of series:
- 优化的结构:通常为数字数据,其中包括
np.datetime64
和bool
. -
object
dtype:用于具有混合类型或不能在NumPy数组中本地保存的类型的系列.该系列的结构是指向任意Python对象的指针序列,通常效率不高.
- Optimised structures: Usually numeric data, this includes
np.datetime64
andbool
. object
dtype: Used for series with mixed types or types which cannot be held natively in a NumPy array. The series is structured as a sequence of pointers to arbitrary Python objects and is generally inefficient.
此序言的原因是,您只需要对第二种类型应用基于元素的逻辑.第一类数据本质上是同类的.
The reason for this preamble is you should only ever need to apply element-wise logic to the second type. Data in the first category is homogeneous by nature.
所以您应该相应地分离逻辑.
So you should separate your logic accordingly.
使用 pd.DataFrame.dtypes
:
print(df.dtypes)
a int64
b float64
c object
dtype: object
object
dtype
通过 pd.DataFrame.select_dtypes
然后使用字典理解:
object
dtype
Isolate these series via pd.DataFrame.select_dtypes
and then use a dictionary comprehension:
obj_types = {col: set(map(type, df[col])) for col in df.select_dtypes(include=[object])}
print(obj_types)
{'c': {int, datetime.time, float}}
您将需要做更多的工作才能获得所需的 exact 格式,但是以上内容应作为您的攻击计划.
You will need to do a little more work to get the exact format you require, but the above should be your plan of attack.
这篇关于如何检测 pandas 对象列中的子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!