【问题标题】:running python with sublime: dtype output is not stable使用 sublime 运行 python:dtype 输出不稳定
【发布时间】:2019-01-25 17:16:47
【问题描述】:

我多次尝试运行以下代码。输出有时是“真”(我所期望的),有时是“假”。崇高有什么问题吗?我用 jupyter notebook 对其进行了测试,输出始终为“True”。

import pandas as pd 

df = pd.DataFrame({'a':[1,2,3]})
print(df.dtypes.isin(['int64']))

如果我打印 df.dtypes,输出总是'int64'。

print(df.dtypes)
>> a    int64
   dtype: object

Python 版本:anaconda python3.6。崇高版本:3.1.1。熊猫版本:0.23.4

【问题讨论】:

  • @Chris 感谢您的评论。将 'int64' 更改为 np.dtype('int64') 似乎有所帮助。但在我的 jupyter notebook 测试中,isin(['int64']) 的返回值确实是“真”。我不知道为什么。可能是因为numpy的版本不同。

标签: python pandas sublimetext3


【解决方案1】:

您示例的输出不是True,而是False。如果你这样做 df.dtypes.values 你会看到它不是字符串 'int64' 它是 dtype('int64') 所以 isin(['int64']) 应该总是返回 False 我在 Jupyter 测试并且输出总是 False

df = pd.DataFrame({'a':[1,2,3]})
print(df.dtypes.isin(['int64']))

a    False
dtype: bool

print(df.dtypes.values)
array([dtype('int64')], dtype=object)

解决方法是将df.dtypes转换成str

df.dtypes.apply(str).values

array(['int64'], dtype=object)

所以你现在可以做isin:

df.dtypes.apply(str).isin(['int64'])

a    True
dtype: bool

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 2019-06-08
    • 2012-01-08
    • 2022-12-17
    • 2014-10-17
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多