Pandas DataFrame 中索引和列的级别(深度)数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27857770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number of levels (depth) of index and columns in a Pandas DataFrame
提问by scls
Python Pandas DataFramecan have hierarchical index (MultiIndex) or hierarchical columns.
Python PandasDataFrame可以有分层索引 ( MultiIndex) 或分层列。
I'm looking for a way to know number of levels (depth) of index and columns.
我正在寻找一种方法来了解索引和列的级别数(深度)。
len(df.index.levels)
seems to only work with MultiIndex but it doesn't work with normal index.
似乎只适用于 MultiIndex 但它不适用于普通索引。
Is there an attribute for this (which will works for MultiIndexbut also simple Index) ?
是否有一个属性(适用于MultiIndex但也很简单Index)?
df.index.depth
or
或者
df.columns.depth
will be great.
会很棒。
One example of MultiIndex columns and index:
MultiIndex 列和索引的一个示例:
import pandas as pd
import numpy as np
def mklbl(prefix,n):
return ["%s%s" % (prefix,i) for i in range(n)]
def mi_sample():
miindex = pd.MultiIndex.from_product([mklbl('A',4),
mklbl('B',2),
mklbl('C',4),
mklbl('D',2)])
micolumns = pd.MultiIndex.from_tuples([('a','foo'),('a','bar'),
('b','foo'),('b','bah')],
names=['lvl0', 'lvl1'])
dfmi = pd.DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),
index=miindex,
columns=micolumns).sortlevel().sortlevel(axis=1)
return(dfmi)
df = mi_sample()
So df looks like:
所以 df 看起来像:
lvl0 a b
lvl1 bar foo bah foo
A0 B0 C0 D0 1 0 3 2
D1 5 4 7 6
C1 D0 9 8 11 10
D1 13 12 15 14
C2 D0 17 16 19 18
D1 21 20 23 22
C3 D0 25 24 27 26
D1 29 28 31 30
B1 C0 D0 33 32 35 34
D1 37 36 39 38
C1 D0 41 40 43 42
D1 45 44 47 46
C2 D0 49 48 51 50
D1 53 52 55 54
C3 D0 57 56 59 58
D1 61 60 63 62
A1 B0 C0 D0 65 64 67 66
D1 69 68 71 70
C1 D0 73 72 75 74
D1 77 76 79 78
C2 D0 81 80 83 82
D1 85 84 87 86
C3 D0 89 88 91 90
D1 93 92 95 94
B1 C0 D0 97 96 99 98
D1 101 100 103 102
C1 D0 105 104 107 106
D1 109 108 111 110
C2 D0 113 112 115 114
D1 117 116 119 118
... ... ... ... ...
A2 B0 C1 D0 137 136 139 138
D1 141 140 143 142
C2 D0 145 144 147 146
D1 149 148 151 150
C3 D0 153 152 155 154
D1 157 156 159 158
B1 C0 D0 161 160 163 162
D1 165 164 167 166
C1 D0 169 168 171 170
D1 173 172 175 174
C2 D0 177 176 179 178
D1 181 180 183 182
C3 D0 185 184 187 186
D1 189 188 191 190
A3 B0 C0 D0 193 192 195 194
D1 197 196 199 198
C1 D0 201 200 203 202
D1 205 204 207 206
C2 D0 209 208 211 210
D1 213 212 215 214
C3 D0 217 216 219 218
D1 221 220 223 222
B1 C0 D0 225 224 227 226
D1 229 228 231 230
C1 D0 233 232 235 234
D1 237 236 239 238
C2 D0 241 240 243 242
D1 245 244 247 246
C3 D0 249 248 251 250
D1 253 252 255 254
[64 rows x 4 columns]
回答by Primer
To make summarized version of comments above:
要制作上述评论的汇总版本:
You can use .nlevelsattribute which gives the number of levels for an index and columns:
您可以使用.nlevels提供索引和列的级别数的属性:
df = pd.DataFrame(np.random.rand(2,2), index=[['A','A'],['B','C']], columns=['a','b'])
df
a b
A B 0.558 0.336
C 0.148 0.436
df.index.nlevels
2
df.columns.nlevels
1
As @joris mentioned above len(df.columns.levels)will not work in the example above as columnsis not MultiIndex, giving:
正如上面提到的@jorislen(df.columns.levels)不会在上面的例子中工作,因为columnsis not MultiIndex,给出:
AttributeError: 'Index' object has no attribute 'levels'
But it will work fine for indexin the example above:
但index在上面的例子中它可以正常工作:
len(df.index.levels)
2
回答by 0xF
You probably need something like len(set(df.a)) which works on both index or normal column.
您可能需要 len(set(df.a)) 之类的东西,它适用于索引或普通列。

