我有以下数据框:
1 2 3 4 5 6 7 8 9 10
cat cat 1 1 1 1 1 1 0 0 1 1
dog 1 0 1 1 1 1 0 0 1 1
fox 0 0 0 0 0 0 0 0 0 0
jumps 1 0 1 1 1 0 0 1 1 1
over 1 0 1 1 1 1 0 0 1 1
the 1 0 1 1 1 1 0 0 1 1
dog cat 1 1 0 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 0 1 1
fox 1 1 1 1 1 1 0 0 1 1
jumps 1 1 1 1 1 1 0 1 1 1
over 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
fox cat 0 0 0 0 0 0 0 0 0 0
dog 1 1 1 1 1 1 0 0 1 1
fox 1 1 1 1 1 1 0 0 1 1
jumps 1 1 1 1 1 1 0 1 1 1
over 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
jumps cat 1 1 0 1 0 1 1 0 1 0
dog 1 1 1 1 1 1 1 0 1 0
fox 1 1 1 1 1 1 1 0 1 0
jumps 1 1 1 1 1 1 0 0 1 0
over 1 0 1 1 1 0 0 1 1 0
the 1 0 1 1 1 1 0 0 1 0
over cat 1 1 0 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 0 1 0
fox 1 1 1 1 1 1 0 0 1 0
jumps 1 1 0 1 0 1 1 0 1 0
over 1 1 1 1 1 1 0 0 1 0
the 1 0 1 1 1 0 0 1 1 0
the cat 1 1 0 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 1 1 0
fox 1 1 1 1 1 1 0 1 1 0
jumps 1 1 0 1 1 1 0 0 1 0
over 1 1 0 1 0 1 1 0 1 0
the 1 1 1 1 1 1 0 0 1 0
正如您所看到的,前两列未标记。我想选择column1 =='dog'
的所有行所以我最终得到了这个:
dog cat 1 1 0 1 1 1 0 0 1 0
dog dog 1 1 1 1 1 1 0 0 1 1
dog fox 1 1 1 1 1 1 0 0 1 1
dog jumps 1 1 1 1 1 1 0 1 1 1
dog over 1 1 1 1 1 1 0 0 1 1
dog the 1 1 1 1 1 1 1 0 1 1
如果有标签,解决方案就是:
print(df.loc[df['label'] == 'dog'])
但是因为它没有标签,我该如何实现呢? 任何建议将受到高度赞赏。谢谢。
答案 0 :(得分:1)
你想要做的是使用这样的双括号:
df.loc[['dog']]
输出:
1 2 3 4 5 6 7 8 9 10
dog cat 1 1 0 1 1 1 0 0 1 0
dog 1 1 1 1 1 1 0 0 1 1
fox 1 1 1 1 1 1 0 0 1 1
jumps 1 1 1 1 1 1 0 1 1 1
over 1 1 1 1 1 1 0 0 1 1
the 1 1 1 1 1 1 1 0 1 1
然后你可以reset_index
:
df.loc[['dog']].reset_index()
输出:
level_0 level_1 1 2 3 4 5 6 7 8 9 10
0 dog cat 1 1 0 1 1 1 0 0 1 0
1 dog dog 1 1 1 1 1 1 0 0 1 1
2 dog fox 1 1 1 1 1 1 0 0 1 1
3 dog jumps 1 1 1 1 1 1 0 1 1 1
4 dog over 1 1 1 1 1 1 0 0 1 1
5 dog the 1 1 1 1 1 1 1 0 1 1
Pandas是MultiIndex
上非常好的文档