Python|熊猫 dataframe.count()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.count()
用于计算编号。给定轴上的非 NA/空观测值。它也适用于非浮动类型数据。
Syntax: DataFrame.count(axis=0, level=None, numeric_only=False)
Parameters:
axis : 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a DataFrame
numeric_only : Include only float, int, boolean data
Returns: count : Series (or DataFrame if level specified)
示例 #1:使用count()
函数在行轴上查找非 NA/null 值的数量。
# importing pandas as pd
import pandas as pd
# Creating a dataframe using dictionary
df = pd.DataFrame({"A":[-5, 8, 12, None, 5, 3],
"B":[-1, None, 6, 4, None, 3],
"C:["sam", "haris", "alex", np.nan, "peter", "nathan"]})
# Printing the dataframe
df
现在在行轴上找到非 NA 值的计数
# axis = 0 indicates row
df.count(axis = 0)
输出 :
示例 #2:使用count()
函数查找列中非 NA/null 值的数量。
# importing pandas as pd
import pandas as pd
# Creating a dataframe using dictionary
df = pd.DataFrame({"A":[-5, 8, 12, None, 5, 3],
"B":[-1, None, 6, 4, None, 3],
"C:["sam", "haris", "alex", np.nan, "peter", "nathan"]})
# Find count of non-NA across the columns
df.count(axis = 1)
输出 :