检测数据帧是否具有 MultiIndex [英] Detect whether a dataframe has a MultiIndex

查看:51
本文介绍了检测数据帧是否具有 MultiIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一种将 DataFrame 解析为 Vincent 兼容格式的新方法.这需要一个标准的Index(Vincent 无法解析MultiIndex).

I am building a new method to parse a DataFrame into a Vincent-compatible format. This requires a standard Index (Vincent can't parse a MultiIndex).

有没有办法检测 Pandas DataFrame 是否有 MultiIndex?

Is there a way to detect whether a Pandas DataFrame has a MultiIndex?

In: type(frame)
Out: pandas.core.index.MultiIndex

我试过了:

In: if type(result.index) is 'pandas.core.index.MultiIndex':
        print True
    else:
        print False
Out: False

如果我尝试不使用引号,我会得到:

If I try without quotations I get:

NameError: name 'pandas' is not defined

感谢任何帮助.

(一旦我有了 MultiIndex,我将重置索引并将两列合并为一个字符串值以用于演示阶段.)

(Once I have the MultiIndex, I'm then resetting the index and merging the two columns into a single string value for the presentation stage.)

推荐答案

您可以使用 isinstance 来检查一个对象是否是一个类(或其子类):

You can use isinstance to check whether an object is a class (or its subclasses):

if isinstance(result.index, pandas.MultiIndex):

这篇关于检测数据帧是否具有 MultiIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆