我正在构建一个新的方法来解析一个DataFrame与Vincent兼容的格式.这需要一个标准Index(文森特不能解析MultiIndex).
有没有办法检测熊猫DataFrame是否有MultiIndex?
In: type(frame)
Out: pandas.core.index.MultiIndex
Run Code Online (Sandbox Code Playgroud)
我试过了:
In: if type(result.index) is 'pandas.core.index.MultiIndex':
print True
else:
print False
Out: False
Run Code Online (Sandbox Code Playgroud)
如果我尝试没有引用,我得到:
NameError: name 'pandas' is not defined
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏.
(一旦我有了MultiIndex,我就会重置索引并将两列合并为表示阶段的单个字符串值.)
jon*_*rpe 20
您可以isinstance用来检查对象是否是类(或其子类):
if isinstance(result.index, pandas.core.index.MultiIndex):
Run Code Online (Sandbox Code Playgroud)
k0r*_*nik 10
您可以使用nlevels来检查有多少个级别:
df.index.nlevels
df.columns.nlevels
Run Code Online (Sandbox Code Playgroud)
如果nlevels > 1,您的数据框肯定有多个索引。
还有
len(result.index.names) > 1
Run Code Online (Sandbox Code Playgroud)
但它比 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
Run Code Online (Sandbox Code Playgroud)