Pandas Groupby多列比较值

发布于 2025-02-09 19:59:05 字数 690 浏览 2 评论 0原文

我的DF看起来像这样:( DF中还有许多其他列,但这是我重点关注的三个)

Param    Value      Limit  
A        1.50       1
B        2.50       1
C        2.00       2
D        2.00       2.5
E        1.50       2

。 ,希望获得这样的列表:

Param    Count       
A        1
B        1       
C        1       
D        0       
E        0       

我尝试了一些方法,第一个是

value_count = df.loc [df ['value']< df ['limit']]。count() 但这只是给出DF中每列的全部计数。

我还尝试了GroupBy函数,我认为通过使用选定的列创建DF的子集,

df_below_limit = df[df['Value'] < df['Limit']]
df_below_limit.groupby('Param')['Value'].count()

这几乎是我想要的,但它不包括我所需要的值。不确定如何根据需要获取清单。

My df looks like this: (There are dozens of other columns in the df but these are the three I am focused on)

Param    Value      Limit  
A        1.50       1
B        2.50       1
C        2.00       2
D        2.00       2.5
E        1.50       2

I am trying to use pandas to calculate how many [Value] that are less than [Limit] per [Param], Hoping to get a list like this:

Param    Count       
A        1
B        1       
C        1       
D        0       
E        0       

I've tried with a few methods, the first being

value_count = df.loc[df['Value'] < df['Limit']].count()
but this just gives the full count per column in the df.

I've also tried groupby function which I think could be the correct idea, by creating a subset of the df with the chosen columns

df_below_limit = df[df['Value'] < df['Limit']]
df_below_limit.groupby('Param')['Value'].count()

This is nearly what I want but it excludes values below which I also need. Not sure how to go about getting the list as I need it.

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

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

发布评论

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

评论(1

站稳脚跟 2025-02-16 19:59:05

假设您需要按参数计数,则可以使用:

out = df['Value'].ge(df['Limit']).groupby(df['Param']).sum()

输出:

Param
A    1
B    2
C    1
D    0
E    0
dtype: int64

二手输入(示例重复的行“ b”):

  Param  Value  Limit
0     A    1.5    1.0
1     B    2.5    1.0
2     B    2.5    1.0
3     C    2.0    2.0
4     D    2.0    2.5
5     E    1.5    2.0
作为dataframe
df['Value'].ge(df['Limit']).groupby(df['Param']).sum().reset_index(name='Count')

# or

df['Value'].ge(df['Limit']).groupby(df['Param']).agg(Count='sum').reset_index()

输出:

  Param  Count
0     A      1
1     B      2
2     C      1
3     D      0
4     E      0

Assuming you want the count per Param, you can use:

out = df['Value'].ge(df['Limit']).groupby(df['Param']).sum()

output:

Param
A    1
B    2
C    1
D    0
E    0
dtype: int64

used input (with a duplicated row "B" for the example):

  Param  Value  Limit
0     A    1.5    1.0
1     B    2.5    1.0
2     B    2.5    1.0
3     C    2.0    2.0
4     D    2.0    2.5
5     E    1.5    2.0
as DataFrame
df['Value'].ge(df['Limit']).groupby(df['Param']).sum().reset_index(name='Count')

# or

df['Value'].ge(df['Limit']).groupby(df['Param']).agg(Count='sum').reset_index()

output:

  Param  Count
0     A      1
1     B      2
2     C      1
3     D      0
4     E      0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文