major minor col
0 0 5
1 6
2 4
0 0 8
1 5
2 6
1 0 3
1 6
2 9
1 0 5
1 1
2 7
First I'd like to get
major minor col
0 0 5
1 6
2 4
0 0 8
1 5
2 6
and then select over both major '0's, that is, choose the first major 0 or the second:
major minor col
0 0 5
1 6
2 4
or
major minor col
0 0 8
1 5
2 6
Unfortunately df.xs(0,level=0,drop_level=False)
doesn't exactly fit the job, since it maintains major '1's in the index, although empty. Any ideas?
I still do not understand your data structure. I'm right now working with
val
major minor col
0 0 5 1
1 6 1
2 4 1
0 8 1
1 5 1
2 6 1
I still don't understand how in your case you have two major zeros, since I get only one with the same structure. Therefore, I can't tell you exactly how you could pick any of the *major*s.
Using traditional slicing, you can get where df.major == 0
using
df[df.major == 0]
In order to select any single of the subgroups now, it depends on how they're different. Do they have another unique feature? Then you could do
df[(df.major == 0) && (df.someColumn == someValue)]
(notice the brackets). Otherwise, if you know there are 3
rows per group, df[df.major == 0].iloc[:3]
(or 3:
) will give you the records.
Also, have a look at the (currently experimental) df.query()
( documentation ).
Generally, you can do stuff such as
df[df.major == 0]
to get all the values where the major is zero. If it's a (labeled) index or a normal column does not matter. You can also stack these to do
`df[(df.major == 0)
I start with
In[264]: df
Out[262]:
val
major minor col
0 0 5 1
1 6 1
2 4 1
0 8 1
1 5 1
2 6 1
1 1 3 1
6 1
2 9 1
1 5 1
1 1
2 7 1
and then I do
In[263]: df.query('major == 0')
Out[261]:
val
major minor col
0 0 5 1
1 6 1
2 4 1
0 8 1
1 5 1
2 6 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.