In the below multiindex dataframe, how can I slice it so that I select rows where the index sign
== 1 and the column csum_count == 8
?
In [15]: raw3.head(20)
Out[15]:
csum_count mean_return
sign cumsum
1 326 9 0.165
854 9 0.081
1346 9 0.055
1440 9 0.157
1554 9 0.069
418 8 0.082
578 8 0.119
638 8 0.113
896 8 0.076
1480 8 0.059
54 7 0.085
446 7 0.120
476 7 0.071
484 7 0.094
594 7 0.089
622 7 0.069
644 7 0.061
1018 7 0.080
1550 7 0.085
1736 7 0.201
I'm able to select all rows with sign == 1
via the following: raw3.loc[(-1,)]
. How do I add the additional criteria that csum_count == 8?
. Thought that raw3.loc[(-1,),'csum_count'==8]
might work but it didn't.
You can filter multi indices by calling DataFrame.index.get_level_values()
and passing it the name (or level) of the index you want to slice on. In addition to that, you can add a condition on one of the columns.
raw3[(raw3.index.get_level_values('sign') == 1) & (raw3[csum_count == 8])]
You can use the.query() method for DataFrames, and pass a boolean string.
In your case, raw3.query('sign == 1 and cumsum == 8')
should give you the result you want.
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.