[英]get unique values in a 3rd column by filtering 2 other columns in Pandas
I have a dataframe i would like to filter.我有一个 dataframe 我想过滤。 Consider the below input dataframe.考虑以下输入 dataframe。
a b c
1 1 1
1 0 1
2 2 1
2 2 2
There are 3 columns ( a, b, and c)有 3 列(a、b 和 c)
I would like to get the count of unique values for c, for each unique pair of a and b, for a new d column, which has the count of unique values in c for its a, b pair:我想获取 c 的唯一值计数,对于 a 和 b 的每对唯一值,对于一个新的 d 列,它的 a,b 对具有 c 中的唯一值计数:
a b c d
1 1 1 1
1 0 1 1
1 2 1 2
1 2 2 2
rows 0, 1 have different a,b column pairs, and so the appended d values for both rows would be 1.第 0、1 行具有不同的 a、b 列对,因此这两行附加的 d 值都为 1。
rows 2 and 3 have shared a, b columns and 2 unique values for that pair, their d values would be 2第 2 行和第 3 行共享 a、b 列和该对的 2 个唯一值,它们的 d 值将为 2
I think you want to use groupby and nunique我想你想使用 groupby 和 nunique
import pandas as pd
data = pd.DataFrame({
'a':[1,1,2,2],
'b':[1,0,2,2],
'c':[1,1,2,3]
})
unique_count = data.groupby(
['a','b']
).c.nunique()
data.set_index(['a','b']).assign(
d = unique_count
).reset_index()
Output: Output:
a b c d
1 1 1 1
1 0 1 1
2 2 2 2
2 2 3 2
Let us try让我们试试
df['cnt'] = df.groupby(['a','b'])['c'].transform('nunique')
df
Out[303]:
a b c cnt
0 1 1 1 1
1 1 0 1 1
2 2 2 1 2
3 2 2 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.