熊猫在两个分位数之间的组中挑选值

发布于 2025-01-29 22:13:27 字数 848 浏览 1 评论 0原文

我想通过选择每个组之间的两个值(dinamsin定义为分位数)之间的行来过滤我的数据集 。具体而言,我有一个数据集,例如

import pandas as pd
df = pd.DataFrame({'day': ['one', 'one', 'one', 'one', 'one', 'one', 'two', 'two', 'two', 'two', 'two'], 
                   'weather': ['rain', 'rain', 'rain', 'sun', 'sun', 'sun', 'sun', 'rain', 'rain', 'sun', 'rain'], 
                   'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]})

”在此处输入图像描述“

我想选择每天的值在0.1和0.9分位数之间的行,每个we仪。我可以通过计算分位数,

df.groupby(['day', 'weather']).quantile([0.1, .9])

但是我感到卡住了。将结果数据集加入原始数据集(原始数据集可能很大),我想知道是否有一些东西

df.groupby(['day', 'weather']).select('value', between=[0.1, 0.9])

I'd like to filter my dataset by picking rows that are between two values (dinamically defined as quantiles) per each group. Concretely, I have a dataset like

import pandas as pd
df = pd.DataFrame({'day': ['one', 'one', 'one', 'one', 'one', 'one', 'two', 'two', 'two', 'two', 'two'], 
                   'weather': ['rain', 'rain', 'rain', 'sun', 'sun', 'sun', 'sun', 'rain', 'rain', 'sun', 'rain'], 
                   'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]})

enter image description here

I'd like to select the rows where the values are between the 0.1 and 0.9 quantile per each day and per each weater. I can calculate the quantiles via

df.groupby(['day', 'weather']).quantile([0.1, .9])

But then I feel stuck. Joining the resulting dataset with the original one it's a waste (the original dataset can be quite big), and I am wondering if there is something along the lines of

df.groupby(['day', 'weather']).select('value', between=[0.1, 0.9])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2025-02-05 22:13:27

变换valueQuantile

g = df.groupby(['day', 'weather'])['value']
df[df['value'].between(g.transform('quantile', 0.1), g.transform('quantile', 0.9))]

   day weather  value
1  one    rain      2
4  one     sun      5
8  two    rain      9

Transform value with quantile

g = df.groupby(['day', 'weather'])['value']
df[df['value'].between(g.transform('quantile', 0.1), g.transform('quantile', 0.9))]

   day weather  value
1  one    rain      2
4  one     sun      5
8  two    rain      9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文