Pandas Groupby多列比较值
我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您需要按参数计数,则可以使用:
输出:
二手输入(示例重复的行“ b”):
作为dataframe
输出:
Assuming you want the count per Param, you can use:
output:
used input (with a duplicated row "B" for the example):
as DataFrame
output: