Pandas DataFrame の describe メソッド

Beginner

はじめに

この実験では、Pandas ライブラリの describe() メソッドを使用して DataFrame の記述統計量を生成する方法を学びます。describe() メソッドは、数値列の件数、平均、標準偏差、最小値、最大値、パーセンタイルなどのさまざまな統計量を計算します。また、オブジェクトデータ型の列に対しても要約統計量を提供します。

VM のヒント

VM の起動が完了したら、左上隅をクリックして Notebook タブに切り替え、Jupyter Notebook を使って練習しましょう。

時々、Jupyter Notebook が読み込み終わるまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題がある場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

必要なライブラリをインポートして DataFrame を作成する

まず、import キーワードを使用して Pandas ライブラリをインポートします。サンプルデータを使って pd.DataFrame() メソッドを使って DataFrame を作成します。

import pandas as pd

## Create a DataFrame
df = pd.DataFrame([['Abhishek', 100, 'Science', 90],
                   ['Anurag', 101, 'Science', 85],
                   ['Chetan', 103, 'Maths', 75]],
                  columns=['Name', 'Roll No', 'Subject', 'Marks'])

describe() メソッドを使って DataFrame を記述する

DataFrame を記述するには、DataFrame オブジェクトに対して describe() メソッドを使用します。

## Describe the DataFrame
description = df.describe()

## Print the description
print(description)

DataFrame のすべての列を記述する

数値型とオブジェクト型の両方を含む DataFrame のすべての列を記述するには、describe() メソッドの include='all' パラメータを使用します。

## Describe all columns of the DataFrame
description_all_columns = df.describe(include='all')

## Print the description of all columns
print(description_all_columns)

DataFrame の特定の列を記述する

DataFrame の特定の列を記述するには、それを属性としてアクセスして describe() メソッドを使用します。

## Describe a specific column of the DataFrame
marks_description = df.Marks.describe()

## Print the description of the 'Marks' column
print(marks_description)

記述から数値列を除外する

記述から数値列を除外するには、describe() メソッドの exclude=np.number パラメータを使用します。

import numpy as np

## Exclude numeric columns from the description
description_exclude_numeric = df.describe(exclude=np.number)

## Print the description excluding numeric columns
print(description_exclude_numeric)

None 値を持つ DataFrame を記述する

None 値を含む DataFrame を記述するには、describe() メソッドが適切に処理します。

## Create a DataFrame with None values
df_with_none = pd.DataFrame([['Abhishek', 101, 'Science', None],
                             ['Anurag', None, 'Science', 85],
                             ['Chetan', None, 'Maths', 75]],
                            columns=['Name', 'Roll No', 'Subject', 'Marks'])

## Describe the DataFrame with None values
description_with_none = df_with_none.describe()

## Print the description of the DataFrame with None values
print(description_with_none)

まとめ

おめでとうございます!この実験では、Pandas の describe() メソッドを使って DataFrame の記述統計量を生成する方法を学びました。この方法を使うと、データセットの分布の中心傾向、分散、形状に関する貴重な洞察を得ることができます。describe() メソッドは、データ分析と探索に強力なツールです。楽しいコーディングを!