我有一个数据框,它的行和列都标有索引:
0 1 1 14 14 14 14 ... 327 327 327 327 327 327 327
14 0 0 0 0 1 1 1 ... 0 0 0 0 0 0 0
14 0 0 0 1 1 1 1 ... 0 0 0 0 0 0 0
14 0 0 0 0 1 1 1 ... 0 0 0 0 0 0 0
14 0 0 0 1 1 1 1 ... 0 0 0 0 0 0 0
14 0 0 0 1 1 0 1 ... 0 0 0 0 0 0 0
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
327 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
327 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
327 0 0 0 0 0 0 0 ... 1 1 0 0 0 1 1
327 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
327 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1
[18349 rows x 1233 columns]
如果我想访问标有 14 的行的元素,命令是:
df.loc['14']
我想获取循环中的元素,其中要循环的变量是行的索引。 我试过的是:
for i in labels:
a = df.loc[str(i)]
其中 labels 是标签的向量,但这不起作用。 有什么建议吗?
答案 0 :(得分:1)
如果要在每个循环中对子数据框进行分组,则需要循环遍历唯一索引。
for index in df.index.unique():
subdf = df.loc[index]
但是,如果您想遍历列中的唯一索引,则需要将两个数组相交。
for index in df.index.unique().intersection(df.columns):
# this index is found both on rows and columns
subdf = df.loc[index]