1#for single row
2df.loc[ index , : ]
3
4# for multiple rows
5indices = [1, 20, 33, 47, 52 ]
6new_df= df.iloc[indices, :]
1### w3sources ###
2d = {'num_legs': [4, 4, 4, 2, 2],
3 'num_wings': [0, 0, 0, 2, 2],
4 'class': ['mammal', 'mammal', 'mammal', 'bird', 'bird'],
5 'animal': ['tiger', 'lion', 'fox', 'eagle', 'penguin'],
6 'locomotion': ['walks', 'walks', 'walks', 'flies', 'walks']}
7
8df = pd.DataFrame(data=d)
9df = df.set_index(['class', 'animal', 'locomotion'])
10
11# num_legs num_wings
12# class animal locomotion
13# mammal| tiger walks 4 0
14# | lion walks 4 0
15# | fox walks 4 0
16# __________________________________________
17# bird | eagle flies 2 2
18# | penguin walks 2 2
19
20df.xs('mammal')
21df.xs(('mammal', 'fox'))
22df.xs('lion', level=1)
23df.xs(('bird', 'walks'),level=[0, 'locomotion'])