[英]Pandas Groupby Multiple Groups Per Row
As a follow-up to my question on multicriteria groupings in pandas , how would you go about grouping rows if one row could belong to multiple groups? 作为我对大熊猫中多标准分组问题的跟进,如果一行可以属于多个组,您将如何对行进行分组?
For examples, if we have 例如,如果我们有
| index | address | first_name | last_name |
| 1 | 1800 St. | John | Adams |
| 2 | 1800 Street | Juno | A. |
| 3 | Elm St. | Junhui | Wen |
| 4 | NaN | Jun | W |
Then 然后
For the first case you could do something like this: 对于第一种情况,您可以执行以下操作:
df.groupby(lambda d: d.address.str.slice(0,3))
In the second case it is the same but with the proper column: 在第二种情况下相同,但具有适当的列:
df.groupby(lambda d: d.first_name.str.slice(0,3))
Let me know if it works :D 让我知道它是否有效:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.