简体   繁体   English

统计一个值在dataframe(多列)中出现的频率

[英]Count the frequency that a value occurs in a dataframe (multiple column)

I want to count the frequency of a value that are same in 2 column, also adding a column at the end that display the counting number & delete the first cloumn.我想计算 2 列中相同值的频率,还在末尾添加一列显示计数并删除第一列。

The dataframe I have dataframe 我有

| Column A | Column B | Column C |
| -------- | -------- | -------- |
| Column A | Cat      | Fish     |
| Column A | Cat      | Apple    |
| Column A | Cat      | Apple    |
| Column A | Dog      | Lemon    |
| Column A | Dog      | Fish     |
| Column A | Dog      | Fish     |

The expected outcome is like预期的结果就像

| Column A | Column B | Column C | 
| -------- | -------- | -------- | 
| Cat      | Fish     |     1    |
| Cat      | Apple    |     2    |
| Dog      | Lemon    |     1    |
| Dog      | Fish     |     2    |

I have tried the我试过

df['Column B'].value_counts()

But I don't know to handle 2 cloumn at the same time.但我不知道同时处理 2 个 cloumn。

You can use GroupBy.count :您可以使用GroupBy.count

out = (
        df.groupby(["Column B", "Column C"],
                   as_index=False, sort=False)
        ["Column A"].count()
       )

# Output: #Output:

print(out)
     Column B    Column C  Column A
0   Cat         Fish              1
1   Cat         Apple             2
2   Dog         Lemon             1
3   Dog         Fish              2

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

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