我正在构建一种新方法来将DataFrame
解析为与Vincent兼容的格式。这需要标准Index
(Vincent无法解析MultiIndex
)。
有没有办法检测大熊猫DataFrame
是否有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
如果我试着没有引用,我会得到:
NameError: name 'pandas' is not defined
任何帮助表示感谢。
(一旦我有MultiIndex
,我就会重置索引并将两列合并为一个表示阶段的单个字符串值。)
答案 0 :(得分:17)
您可以使用isinstance
检查对象是否是类(或其子类):
if isinstance(result.index, pandas.core.index.MultiIndex):
答案 1 :(得分:5)
您可以使用nlevels
来检查有多少个级别:
df.index.nlevels
df.columns.nlevels
如果为nlevels > 1
,则您的数据框肯定具有多个索引。
答案 2 :(得分:3)
还有
len(result.index.names) > 1
但它比isinstance或者类型慢得多:
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
答案 3 :(得分:0)
也许最短的方式是if type(result.index)==pd.MultiIndex: