Hướng dẫn dùng mode pandas python

DataFrame.mode(axis=0, numeric_only=False, dropna=True)[source]

Get the mode(s) of each element along the selected axis.

The mode of a set of values is the value that appears most often. It can be multiple values.

Parametersaxis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to iterate over while searching for the mode:

  • 0 or ‘index’ : get mode of each column

  • 1 or ‘columns’ : get mode of each row.

numeric_onlybool, default False

If True, only apply to numeric columns.

dropnabool, default True

Don’t consider counts of NaN/NaT.

ReturnsDataFrame

The modes of each column or row.

Examples

>>> df = pd.DataFrame([('bird', 2, 2),
...                    ('mammal', 4, np.nan),
...                    ('arthropod', 8, 0),
...                    ('bird', 2, np.nan)],
...                   index=('falcon', 'horse', 'spider', 'ostrich'),
...                   columns=('species', 'legs', 'wings'))
>>> df
           species  legs  wings
falcon        bird     2    2.0
horse       mammal     4    NaN
spider   arthropod     8    0.0
ostrich       bird     2    NaN

By default, missing values are not considered, and the mode of wings are both 0 and 2. Because the resulting DataFrame has two rows, the second row of species and legs contains NaN.

>>> df.mode()
  species  legs  wings
0    bird   2.0    0.0
1     NaN   NaN    2.0

Setting dropna=False NaN values are considered and they can be the mode (like for wings).

>>> df.mode(dropna=False)
  species  legs  wings
0    bird     2    NaN

Setting numeric_only=True, only the mode of numeric columns is computed, and columns of other types are ignored.

>>> df.mode(numeric_only=True)
   legs  wings
0   2.0    0.0
1   NaN    2.0

To compute the mode over columns and not rows, use the axis parameter:

>>> df.mode(axis='columns', numeric_only=True)
           0    1
falcon   2.0  NaN
horse    4.0  NaN
spider   0.0  8.0
ostrich  2.0  NaN

Để cài đặt Pandas thì chắc chạn bạn cần có Python. Nếu như bạn chưa cài Python thì mình khuyến khích sử dụng Anaconda, nó bao gồm Python, Pandas và các thư viện phổ biến được sử dụng khác (NumPy, Matplotlib, Scipy,...) cho tính toán khoa học và xử lý dữ liệu.

Ngoài ra, Pandas hoàn toàn có thể cài như một package Python bình thường, bạn có thể cài qua Pip bằng câu lệnh:

Hoặc nếu bạn đang dùng conda:

Hoặc bạn muốn cài đặt trên các phiên bản Linux:

Với người mới, chưa có nhiều kinh nghiệm thì cài đặt Pandas nói riêng cũng như Python nói chung khá khó khăn, mình khuyến khích cài đặt pandas theo các bước sau:

  1. Cài đặt Anaconda (giúp cài đặt tất cả các gói bạn cần và tất cả các công cụ khác được đề cập ở dưới).
  2. Để viết code, sử dụng notebooks trong JupyterLab.
  3. Dùng Anaconda Navigator để quản lý các packages sẽ cài.

2. Kiểm tra version của Pandas

Khi đã hoàn tất việc cài đặt Pandas, bạn có thể kiểm tra version pandas (trong thời điểm viết bài này thì pandas version đang là 1.1.3) để xem nó đã được cài đặt ổn định hay chưa bằng cú pháp sau:

import pandas as pd

pd.__version__

Kết quả :