给定以下数据框:
将pandas导入为pddf = pd.DataFrame({'A' : ['foo', 'foo', 'foo', 'foo','bar', 'bar', 'bar', 'bar'],'B' : [2, 4, 4, 2, 5, 4, 3, 2]})df甲乙0 富 21 富 42 富 43 富 24 巴 55 巴 46 巴 37 巴 2我想要一个列 ('C'),其中包含每组 'A' 的 'B' 的唯一值计数,像这样通过 lambda x 函数:
A B C0 富 2 21 富 4 22 富 4 23 富 2 24 巴 5 15 巴 4 16 巴 3 17 巴 2 1提前致谢!
解决方案如果 PC 对你的目标是正确的,也许
>>>df["C"] = df.groupby(["A","B"])["A"].transform("count")>>>df乙丙0 富 2 21 富 4 22 富 4 23 富 2 24 巴 5 15 巴 4 16 巴 3 17 巴 2 1会给你你想要的吗?我们按 (A,B) 对分组.
可爱的历史:这本来就是我所做的,但后来我再次尝试,发现我不需要 ["A"].但是没有它它第二次工作的原因是我当时有 C 列,所以有一些代码可以执行..(叹气)
Given the following data frame:
import pandas as pd df = pd.DataFrame( {'A' : ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar'], 'B' : [2, 4, 4, 2, 5, 4, 3, 2]}) df A B 0 foo 2 1 foo 4 2 foo 4 3 foo 2 4 bar 5 5 bar 4 6 bar 3 7 bar 2I would like a column ('C') of the count of unique values for 'B' per group of 'A' like this via a lambda x function:
A B C 0 foo 2 2 1 foo 4 2 2 foo 4 2 3 foo 2 2 4 bar 5 1 5 bar 4 1 6 bar 3 1 7 bar 2 1Thanks in advance!
解决方案If PC is right about your goal, maybe
>>> df["C"] = df.groupby(["A","B"])["A"].transform("count") >>> df A B C 0 foo 2 2 1 foo 4 2 2 foo 4 2 3 foo 2 2 4 bar 5 1 5 bar 4 1 6 bar 3 1 7 bar 2 1would give you what you want? We're grouping on (A,B) pairs.
Cute bit of history: this is originally what I'd done, but then I tried it again and found I didn't need the ["A"]. But the reason it worked the second time without it is that I had the C column then, so there was something for the code to act on.. (sigh)
更多推荐
Pandas 添加唯一计数列
发布评论