Python 检测数据帧是否具有多索引
我正在构建一种新方法,将Python 检测数据帧是否具有多索引,python,pandas,Python,Pandas,我正在构建一种新方法,将数据帧解析为Vincent兼容的格式。这需要标准的索引(Vincent无法解析多索引) 是否有方法检测数据帧是否具有多索引 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
数据帧
解析为Vincent兼容的格式。这需要标准的索引
(Vincent无法解析多索引
)
是否有方法检测数据帧是否具有多索引
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
如果我尝试不引用报价,我会得到:
NameError: name 'pandas' is not defined
谢谢你的帮助
(一旦我有了
多索引
,我将重置索引并将两列合并为一个字符串值,用于演示阶段。)您可以使用isinstance
检查对象是否为类(或其子类):
如果type(result.index)=pd.MultiIndex:还有
len(result.index.names) > 1
但它比isinstance或type慢得多:
timeit(len(result.index.names) > 1)
The slowest run took 10.95 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.12 µs per loop
In [254]:
timeit(isinstance(result.index, pd.MultiIndex))
The slowest run took 30.53 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 177 ns per loop
In [252]:
)
timeit(type(result.index) == pd.MultiIndex)
The slowest run took 22.86 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 200 ns per loop
您可以使用
nlevels
检查有多少个级别:
df.index.nlevels
df.columns.nlevels
如果
nlevels>1
,您的数据帧肯定有多个索引。对我来说很有用-这让我意识到了我的错误,因为我习惯将熊猫作为“pd”输入以进行速记。此编辑版本适用于我的脚本:if-isinstance(result.index,pd.core.index.MultiIndex):FWIW,至少从版本0.24.1开始,您可以使用if-isinstance(result.index,pandas.MultiIndex):
在pandas 0.24.2中使用:if-isinstance(result.keys(),pd.core.index.multi.MultiIndex):
未定义名称“pandas”
您应该首先导入pandas
!您应该注意到len(result.index.names)>1的单位是微秒,其他单位是纳秒,因此len实际需要1120 ns。啊!谢谢@johncerls,那些讨厌的友好单元转换器一直吸引着我的眼球(boostunittest做了类似的事情)。一个多索引不能只有一个级别吗?为什么不df.index.nlevels
?请注意nlevels==1
也可以用于pd.MultiIndex
类型的索引。如果类型很重要,那么使用isinstance(df,pd.MultiIndex)
进行测试是正确的方法。这个答案的优点是与Python的“duck typing”很好地结合在一起——对我来说重要的是我是必须处理多个级别还是只处理一个级别,而不是特定的实例。在我的机器上,它的速度即使不比isinstance()快,也一样快。
df.index.nlevels
df.columns.nlevels