Pandas数据框架中的计数值

Pandas数据框架中的计数值

在这篇文章中,我们将在Pandas数据框架中计算数值。首先,我们将创建一个数据框,然后我们将计算不同属性的值。

语法: DataFrame.count(axis=0, level=None, numeric_only=False)

参数:

  • axis {0 or ‘index’, 1 or ‘columns’}: default 0 如果 axis=0 或 axis=’index’,将为每一列生成计数;如果 axis=1 或 axis=”columns”,将为每一行生成计数。
  • level(nt或str,可选)。如果轴是一个MultiIndex,沿着一个特定的级别计数,折叠成一个DataFrame。str指的是级别名称。
  • numeric_only(布尔值,默认为False)。它只包括int、float或boolean值。

返回:它返回非空值的数量,如果使用级别,则返回数据帧。

逐步实现:

第1步:导入库。

# importing libraries
import numpy as np
import pandas as pd
Python

第2步:创建数据框架

# Creating dataframe with
# some missing values
NaN = np.nan
dataframe = pd.DataFrame({'Name': ['Shobhit', 'Vaibhav',
                                   'Vimal', 'Sourabh',
                                   'Rahul', 'Shobhit'],
                          'Physics': [11, 12, 13, 14, NaN, 11],
                          'Chemistry': [10, 14, NaN, 18, 20, 10],
                          'Math': [13, 10, 15, NaN, NaN, 13]})
 
display(dataframe)
Python

输出:

Pandas数据框架中的计数值

创建Dataframe

第3步:在这一步中,我们只是简单地使用.count()函数来计算不同列的所有值。

# using dataframe.count()
# to count all values
dataframe.count()
Python

输出:

Pandas数据框架中的计数值

我们可以看到,由于我们有缺失的数值,所以在计数值上存在差异。姓名栏有5个值,物理和化学有4个,数学有3个。在这种情况下,它使用的是它的默认值的一个参数。

第四步:如果我们想计算所有的行的值,那么我们必须通过axis=1或’columns’。

# we can pass either axis=1 or
# axos='columns' to count with respect to row
print(dataframe.count(axis = 1))
 
print(dataframe.count(axis = 'columns'))
Python

输出:

Pandas数据框架中的计数值

相对于行的计数

第5步:现在,如果我们想在我们的数据框架中计算空值。

# it will give the count
# of individual columns count of null values
print(dataframe.isnull().sum())
 
# it will give the total null
# values present in our dataframe
print("Total Null values count: ",
      dataframe.isnull().sum().sum())
Python

输出:

Pandas数据框架中的计数值

第6步: .一些使用.count()的例子

现在我们要计算物理分数大于11分的学生人数。

# count of student with greater
# than 11 marks in physics
print("Count of students with physics marks greater than 11 is->",
      dataframe[dataframe['Physics'] > 11]['Name'].count())
 
# resultant of above dataframe
dataframe[dataframe['Physics']>11]
Python

输出:

Pandas数据框架中的计数值

**物理学>11 **

物理分数大于10,化学分数大于11,数学分数大于9的学生人数。

# Count of students whose physics marks
# are greater than 10,chemistry marks are
# greater than 11 and math marks are greater than 9.
print("Count of students ->",
      dataframe[(dataframe['Physics'] > 10) &
                (dataframe['Chemistry'] > 11) &
                (dataframe['Math'] > 9)]['Name'].count())
 
# dataframe of above result
dataframe[(dataframe['Physics'] > 10 ) &
          (dataframe['Chemistry'] > 11 ) &
          (dataframe['Math'] > 9 )]
Python

输出:

Pandas数据框架中的计数值

以下是完整的实施方案:

# importing Libraries
import pandas as pd
import numpy as np
 
# Creating dataframe using dictionary
NaN = np.nan
dataframe = pd.DataFrame({'Name': ['Shobhit', 'Vaibhav',
                                   'Vimal', 'Sourabh',
                                   'Rahul', 'Shobhit'],
                          'Physics': [11, 12, 13, 14, NaN, 11],
                          'Chemistry': [10, 14, NaN, 18, 20, 10],
                          'Math': [13, 10, 15, NaN, NaN, 13]})
 
print("Created Dataframe")
print(dataframe)
 
# finding Count of all columns
print("Count of all values wrt columns")
print(dataframe.count())
 
# Count according to rows
print("Count of all values wrt rows")
print(dataframe.count(axis=1))
print(dataframe.count(axis='columns'))
 
# count of null values
print("Null Values counts ")
print(dataframe.isnull().sum())
print("Total null values",
      dataframe.isnull().sum().sum())
 
# count of student with greater
# than 11 marks in physics
print("Count of students with physics marks greater than 11 is->",
      dataframe[dataframe['Physics'] > 11]['Name'].count())
 
# resultant of above dataframe
print(dataframe[dataframe['Physics'] > 11])
print("Count of students ->",
      dataframe[(dataframe['Physics'] > 10) &
                (dataframe['Chemistry'] > 11) &
                (dataframe['Math'] > 9)]['Name'].count())
 
print(dataframe[(dataframe['Physics'] > 10) &
                (dataframe['Chemistry'] > 11) &
                (dataframe['Math'] > 9)])
Python

输出:

Pandas数据框架中的计数值

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册