pandas DF中的重复行 [英] Duplicate rows in pandas DF
问题描述
字母数字
A 1
A 3
A 2
A 1
B 1
B 2
B 3
C 2
C 2
我正在寻找类似行的数量,并将结果保存在第三列中。例如,我正在寻找的输出:
字母数字事件
pre>
A 1 2
A 2 1
A 3 1
B 1 1
B 2 1
B 3 1
C 2 2
我想要做的一个例子是此处。我想出的最好的想法是使用
count_values()
,但我认为这只是一列。另一个想法是使用重复()
,反正我不想为 -loop构建任何。我很确定,存在一个for循环的Pythonic替代。
解决方案您可以对这两列进行分组,然后计算组的大小:
在[16]中:df.groupby(['Letters','Numbers'])。 size()
输出[16]:
字母数字
A 1 2
2 1
3 1
B 1 1
2 1
3 1
C 2 2
dtype:int64
获取一个DataFrame,就像你的示例输出一样,你可以用
reset_index
重置索引。I have a DF in Pandas, which looks like:
Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2 B 3 C 2 C 2
I'm looking to count the number of similar rows and save the result in a third column. For example, the output I'm looking for:
Letters Numbers Events A 1 2 A 2 1 A 3 1 B 1 1 B 2 1 B 3 1 C 2 2
An example of what I'm looking to do is here. The best idea I've come up with is to use
count_values()
, but I think this is just for one column. Another idea is to useduplicated()
, anyway I don't want construct anyfor
-loop. I'm pretty sure, that a Pythonic alternative to a for loop exists.解决方案You can groupby these two columns and then calculate the sizes of the groups:
In [16]: df.groupby(['Letters', 'Numbers']).size() Out[16]: Letters Numbers A 1 2 2 1 3 1 B 1 1 2 1 3 1 C 2 2 dtype: int64
To get a DataFrame like in your example output, you can reset the index with
reset_index
.这篇关于 pandas DF中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!