简体   繁体   English

熊猫按两列分组,并从第三列输出值

[英]Pandas groupby two columns and output values from 3rd column

colour    num    accepted  returned
grey      1      yes       no
red       2      no        no
grey      4      yes       yes

I have the dataframe above and want to output unique combinations of colour and num columns and also the corresponding value in returned , as below 我具有上述数据帧和要输出的独特组合colournum列以及在对应的值returned ,如以下

colour    num     returned
grey      1       no    
red       2       no      
grey      4       yes     

Using df.groupby(['colour', 'num']).size() gives me unique combinations but not the returned column. 使用df.groupby(['colour', 'num']).size()给我唯一的组合,但没有returned列。

If you're sure that the combination of colour and num is unique, you can just do: 如果您确定color和num的组合是唯一的,则可以执行以下操作:

df.groupby(['colour', 'num'])['returned'].max()

Of course, if it's not really unique and there is both a 'yes' and 'no' this will return 'yes' because 'yes' > 'no'... 当然,如果它不是唯一的,并且同时存在“是”和“否”,则将返回“是”,因为“是”>“否” ...

But actually, this solution doesn't give anything else than df[['colour','num','returned']].drop_duplicates() , which is definitely leaner. 但是实际上,此解决方案除了df[['colour','num','returned']].drop_duplicates() ,没有其他任何东西,它肯定更精简。

If somehow you know that rows can be repeated but the returned value is unique and you want at the same time the number of times it appears and the unique value in "returned", you can do it in one go with: 如果您以某种方式知道行可以重复, 但是返回的值是唯一的,并且同时希望它出现的次数和“ returned”中的唯一值,则可以一次性完成:

df.groupby(['colour','num'])['returned'].agg(['size','max'])

Which would return: 哪个会返回:

            size    max
colour  num     
grey    1   1       no
        4   1       yes
red     2   1       no

From your description, I think you should to group the returned column as well. 根据您的描述,我认为您也应该对returned列进行分组。

df.groupby(['colour','num','returned']).size() This will display the number of occurences of each returned status, grouped by num and colour : df.groupby(['colour','num','returned']).size()这将显示每个返回状态的出现次数,按numcolour分组:

colour  num  returned
grey    1    no          1
        4    yes         1
red     2    no          1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 groupby 2 列之后从第 3 列获取相应的值 - obtain corresponding values from 3rd column after groupby 2 columns Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column Pandas:如果来自第三列的字符串值,则根据另一列的值创建列 - Pandas : Create columns based on values of another column if string value from 3rd column Python 比较 2 列并用第 3 列中的值写入第 4 列(熊猫) - Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas ) 第 3 列 pandas python 中至少有两列 - Minimum of two columns in a 3rd column pandas python 比较来自相同 pandas dataframe 的 2 列的值和基于比较的第 3 列的返回值 - comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison 按两列分组,求和、计数并在单独的列中显示输出值(熊猫) - Groupby two columns, sum, count and display output values in separate column (pandas) 如何通过对第三列中的值求和,将前两列中具有相同值的 Pandas Dataframe 的行组合在一起? - How to group together rows of Pandas Dataframe with same values in first 2 columns by summing values in the 3rd column? 如何基于两列删除重复数据,从而删除熊猫数据框中第三列中最大的列? - How to remove duplicates based on two columns removing the the largest of 3rd column in pandas dataframe? Pandas Dataframe 使用 Groupby 从其他两列的唯一值创建下一个未来日期的列 - Pandas Dataframe Create Column of Next Future Date from Unique values of two other columns, with Groupby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM