Pandas 添加唯一计数列 [英] Pandas add unique count column
问题描述
给定以下数据框:
将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 2
I 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 1
Thanks 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 1
would 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 添加唯一计数列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!