简体   繁体   English

Pandas 在所有数据帧中添加另一列的计数列

[英]Pandas add column of count of another column across all the datafram

I have a dataframe:我有一个 dataframe:

df = C1  C2  E  
     1    2  3
     4    9  1
     3    1  1 
     8    2  8
     8    1  2

I want to add another columns that will have the count of the value that is in the columns 'E' in all the dataframe (in the column E) So here the output will be:我想添加另一列,这些列将包含所有 dataframe 中“E”列中的值的计数(在 E 列中)所以这里的 output 将是:

df = C1. C2. E. cou 
     1.   2. 3.  1 
     4.   9. 1.  2
     3.   1. 1   2
     8.   2. 8.  1
     8.   1. 2.  1 #2 appears only one it the column E

How can it be done efficiently?如何有效地完成它?

Here's one way.这是一种方法。 Find the matches and add them up.找到匹配项并将它们相加。

import pandas as pd

data = [
    [1,2,3],[4,9,1],[3,1,1],[8,2,8]
]

df = pd.DataFrame( data, columns=['C1','C2','E'])
print(df)

def count(val):
    return (df['C1']==val).sum() + (df['C2']==val).sum()

df['cou'] = df.E.apply(count)
print(df)

Output: Output:

   C1  C2  E
0   1   2  3
1   4   9  1
2   3   1  1
3   8   2  8
   C1  C2  E  cou
0   1   2  3    1
1   4   9  1    2
2   3   1  1    2
3   8   2  8    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM