Pandas 添加唯一计数列

编程入门 行业动态 更新时间:2024-10-25 01:31:49
本文介绍了Pandas 添加唯一计数列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定以下数据框:

将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 添加唯一计数列

本文发布于:2023-10-30 19:05:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数列   Pandas   唯一计

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!