<分区>
我有一个 pandas 数据框 df
,它有一列,称之为 A
,它包含多种数据类型。我想选择 df
的所有行,其中 A
具有特定的数据类型。
例如,假设 A
的类型为 int
和 str
。我想做一些类似 df[type(df[A])==int]
的事情。
<分区>
我有一个 pandas 数据框 df
,它有一列,称之为 A
,它包含多种数据类型。我想选择 df
的所有行,其中 A
具有特定的数据类型。
例如,假设 A
的类型为 int
和 str
。我想做一些类似 df[type(df[A])==int]
的事情。
最佳答案
设置
df = pd.DataFrame({'A': ['hello', 1, 2, 3, 'bad']})
这整列将被分配 dtype
Object
。如果您只想查找数值:
pd.to_numeric(df.A, errors='coerce').dropna()
1 1.0
2 2.0
3 3.0
Name: A, dtype: float64
但是,这也允许混合使用 float 、数字的字符串表示形式等。如果你真的想找到 type
int
的元素,你可以使用列表理解:
df.loc[[isinstance(val, int) for val in df.A], 'A']
1 1
2 2
3 3
Name: A, dtype: object
但是注意 dtype
仍然是 Object
。
如果列有 bool 值,这些将被保留,因为 bool
是 int
的子类。如果您不想要这种行为,您可以使用 type
而不是 isinstance
关于python - 按 dtype 子集 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52561574/