pandas 列中唯一值的计数 [英] Count of unique value in column pandas

查看:113
本文介绍了 pandas 列中唯一值的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,正在查看数据框中的一列,即名称

I have a dataframe and I am looking at one column within the dataframe called names

array(['Katherine', 'Robert', 'Anne', nan, 'Susan', 'other'], dtype=object)

我试图打个电话告诉我这些唯一名称在该列中出现了多少次,例如,如果有223个Katherine实例等. 我该怎么做呢?我知道value_counts仅针对其中每个显示1,因为它们是单独的唯一值

I am trying to make a call to tell me how many times each of these unique names shows up in the column, for example if there are 223 instances of Katherine etc. How do i do this? i know value_counts just shows 1 for each of these because they are the separate unique values

推荐答案

如果我对您的理解正确,则可以使用

If I understand you correctly, you can use pandas.Series.value_counts.

示例:

import pandas as pd
import numpy as np

s = pd.Series(['Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])

s.value_counts()

Katherine    1
Robert       1
other        1
Anne         1
Susan        1
dtype: int64

您提供的数据只有每个名称之一-因此,下面是带有多个凯瑟琳"条目的示例:

The data you provided only has one of each name - so here is an example with multiple 'Katherine' entries:

s = pd.Series(['Katherine','Katherine','Katherine','Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])

s.value_counts()

Katherine    4
Robert       1
other        1
Anne         1
Susan        1
dtype: int64

应用于数据框时,您将按以下方式调用它:

When applied to your Dataframe you will call this as follows:

df['names'].value_counts()

这篇关于 pandas 列中唯一值的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆