Python中的Pandas DataFrame count()方法
Pandas DataFrame.count()函数用于计算给定轴上的非NA /空值的数量。伟大的事情是它也可以与非浮点类型数据一起使用。 df.count()函数在Pandas库中定义。 Pandas是Python中的软件包之一,它使用户更容易分析数据。
了解Pandas DataFrame count()
Pandas DataFrame.count()函数用于对给定轴上的非NA /空观测值进行计数。 DataFrame.count()也适用于非浮动类型的数据。 count()函数用于对每一列或每一行的非NA单元进行计数。值None,NaN,NaT和可选的numpy.inf(取决于pandas.options.mode.use_inf_as_na)被视为NA。
句法
DataFrame.count(axis=0, level=None, numeric_only=False)
所有参数均设置为默认值。
参量
熊市猫的dataframe.count()函数具有三个参数。
- 轴:行方向为0,列方向为1。
- 级别:如果轴是Multiindex(分层),则计数与特定级别一起完成,并折叠为DataFrame。
- numeric_only:此参数仅包含float,int和boolean数据。
返回值
熊市猫的count()方法通常返回series,但是当指定级别时,它也可以返回DataFrame。
关于Pandas DataFrame count()的示例程序
编写一个程序来展示Python中count()方法的工作方式。
import pandas as pd import numpy as np # Importing numpy for nan values. # Creating the dataset using dictionary dataset = { 'Name': ['Rohit', 'Arun', 'Sohit', 'Arun', 'Shubh'], 'Roll no': ['01', '02', '03', '04', np.nan], 'Maths': ['93', '63', np.nan, '94', '83'], 'Science': ['88', np.nan, '66', '94', np.nan], 'English': ['93', '74', '84', '92', '87']} df = pd.DataFrame(dataset) print("DataFrame:nn", df) print("nCount:n") # Using the count() df2 = df.count() print(df2)
输出量
DataFrame: Name Roll no Maths Science English 0 Rohit 01 93 88 93 1 Arun 02 63 NaN 74 2 Sohit 03 NaN 66 84 3 Arun 04 94 94 92 4 Shubh NaN 83 NaN 87 Count: Name 5 Roll no 4 Maths 4 Science 3 English 5 dtype: int64
在上面的示例中,我们可以看到我们创建了一个包含学生数据的数据框,然后打印了该数据框。之后,我们使用count()函数获取每一列中的值计数(其中存在的元素数)。
一件事,您会注意到count()函数没有计算NaN值。
编写一个程序以使用count()按行显示计数。
import pandas as pd import numpy as np # Importing numpy for NaN values # Creating dataset using dictionary dataset = { 'Name': ['Rohit', 'Arun', 'Sohit', 'Arun', 'Shubh'], 'Roll no': ['01', '02', '03', '04', np.nan], 'Maths': ['93', '63', np.nan, '94', '83'], 'Science': ['88', np.nan, '66', '94', np.nan], 'English': ['93', '74', '84', '92', '87']} df = pd.DataFrame(dataset) print("DataFrame:nn", df) print("nCount:n") # Using the count() function df2 = df.count(axis="columns") print(df2)
输出量
DataFrame: Name Roll no Maths Science English 0 Rohit 01 93 88 93 1 Arun 02 63 NaN 74 2 Sohit 03 NaN 66 84 3 Arun 04 94 94 92 4 Shubh NaN 83 NaN 87 Count: 0 5 1 4 2 4 3 5 4 3 dtype: int64
在上面的示例中,我们可以看到我们创建了一个包含学生数据的数据框,然后打印了该数据框。之后,我们使用count()函数逐行获取计数。
计数一级的MultiIndex
import pandas as pd import numpy as np # Importing numpy for NaN values # Creating dataset using dictionary dataset = { 'Name': ['Rohit', 'Arun', 'Sohit', 'Arun', 'Shubh'], 'Roll no': ['01', '02', '03', '04', np.nan], 'Maths': ['93', '63', np.nan, '94', '83'], 'Science': ['88', np.nan, '66', '94', np.nan], 'English': ['93', '74', '84', '92', '87']} df = pd.DataFrame(dataset) print("DataFrame:nn", df) print("nCount:n") # Using the count() function df2 = df.set_index(["Maths", "English"]).count(level="Maths") print(df2)
在此示例中,我们使用Pandas set_index()函数设置了多索引。
输出量
DataFrame: Name Roll no Maths Science English 0 Rohit 01 93 88 93 1 Arun 02 63 NaN 74 2 Sohit 03 NaN 66 84 3 Arun 04 94 94 92 4 Shubh NaN 83 NaN 87 Count: Name Roll no Science Maths 63 1 1 0 83 1 0 0 93 1 1 1 94 1 1 1
结论
一旦您知道核心操作以及如何使用数据,Pandas便是处理数据的强大工具。值None,NaN,NaT和可选的numpy.inf(取决于pandas.options.mode.use_inf_as_na)被视为NA。 count()函数对每一列或每一行的非NA单元进行计数。
也可以看看
熊市猫DataFrame位置[]
熊市猫date_range()
熊市猫DataFrame reset_index()
熊市猫to_datetime()
熊市猫to_frame()