python-通过multiIndex之一的最高分位数过滤数据帧行

我有一个带有multiIndex的数据框,我只想保留其列值在级别0索引的最高分位数中的子集记录.

在以下示例df中,我想保留其列A值在每个级别0索引的最高分位数为75%的记录. pythonic的做事方式是什么?

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
    np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]

df = pd.DataFrame(np.random.randn(8, 3), index=arrays, columns = ['A','B','C'])

谢谢

解决方法:

你是这个意思吗?

df.groupby(level=0, group_keys=False).apply(lambda g: g[g.A >= g.A.quantile(0.75)])

enter image description here

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...