python - 按 dtype 子集 Pandas 数据框

标签 python pandas dataframe

<分区>

我有一个 pandas 数据框 df,它有一列,称之为 A,它包含多种数据类型。我想选择 df 的所有行,其中 A 具有特定的数据类型。

例如,假设 A 的类型为 intstr。我想做一些类似 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 值,这些将被保留,因为 boolint 的子类。如果您想要这种行为,您可以使用 type 而不是 isinstance

关于python - 按 dtype 子集 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52561574/

相关文章:

python - URL Dispatch 中路由的多个路径

python - Django 1.7(显然)没有在manage.py测试上运行迁移

python - 如何旋转数据框

python - Pandas 在尝试删除重复项时仅删除某些列值

Python Pandas Dataframe 填充 NaN 值

python - 去除低频词

python - 如何根据时间段评估日志跟踪

python - 在 Windows 中使用 crypt 模块?

python - 如何按顺序获取 pandas 数据框中最高、下一个最高数字等的索引和列?

python - 如何获取2个连续记录之间的 "time difference"?