本文介绍了如何检查python pandas中列内容的dtype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与如何检查python pandas中列的dtype .

创建了一个空的熊猫数据框.之后,它充满了数据.然后,如何检查其任何列中是否包含complex类型?

An empty pandas dataframe is created. Following this, it's filled with data. How can I then check if any of its columns contain complex types?

index = [np.array(['foo', 'qux'])]
columns = ["A",  "B"]
df = pd.DataFrame(index=index, columns=columns)
df.loc['foo']["A"] = 1 + 1j
df.loc['foo']["B"] = 1
df.loc['qux']["A"] = 2
df.loc['qux']["B"] = 2

print df
for type in df.dtypes:
    if type == complex:
        print type

此刻,我得到的类型为object,这是没有用的.

At the moment, I get the type as object which isn't useful.

          A  B
foo  (1+1j)  1
qux       2  2

推荐答案

考虑系列s

s = pd.Series([1, 3.4, 2 + 1j], dtype=np.object)
s

0         1
1       3.4
2    (2+1j)
dtype: object

如果我使用pd.to_numeric,它将复杂的dtype升级为complex

If I use pd.to_numeric, it will upcast the dtype to complex if any are complex

pd.to_numeric(s).dtype

dtype('complex128')

这篇关于如何检查python pandas中列内容的dtype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:22