Python | Панды dataframe.count ()

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas dataframe.count() is used to count the no. of non-NA/null observations across the given axis. It works with non-floating type data as well.

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)

Example #1: Use count() function to find the number of non-NA/null value across the row axis.

# 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

Now find the count of non-NA value across the row axis

# axis = 0 indicates row
df.count(axis = 0)

Output :

 
Example #2: Use count() function to find the number of non-NA/null value across the column.

# 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)

Выход :

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.