diagnose

badge of tested backend

データフレームの概要

概要

R言語の dlookr::diagnose() を再現した関数で、データの全般的な状態についての要約を提供します。

diagnose(data: IntoFrameT, to_native: bool = True)

引数 Argument

  • dataIntoFrameT(必須)
    入力データ。narwhals が受け入れ可能な DataFrame 互換オブジェクト
    (例:pandas.DataFramepolars.DataFramepyarrow.Table)を指定できます。
  • to_native: bool
    True の場合、入力と同じ型のデータフレーム(e.g. pandas / polars / pyarrow)を返します。
    False の場合、narwhals.DataFrame を返します。デフォルトは True で、to_native = False は、主にライブラリ内部での利用や、バックエンドに依存しない後続処理を行う場合を想定したオプションです。

返り値 Value

  • dtype:該当する列のpandasにおけるデータの型。「〇〇の個数」や「〇〇の金額」といったデータの dtypeobjectString になっていたら、文字列として読み込まれているので要注意です。
  • missing_count:1列のなかで NaN などの欠測値になっている数
  • missing_percent:1列のなかで欠測値が占めている割合でmissing_percent = (missing_count / 行数) * 100 として計算されます。もし missing_percent = 100 なら、その列は完全に空白です。
  • unique_count:その列で重複を除外したユニークな値の数。例えばある列の中身が「a, a, b」であればユニークな値は ab の2つなので unique_count = 2 です。もし unique_count = 1 であれば、その行にはたった1種類の値しか含まれていないことが分かりますし、例えば都道府県を表す列の unique_count が47より多ければ、都道府県以外のものが混ざっていると考えられます。
  • unique_rate: サンプルに占めるユニークな値の割合。 unique_rate = unique_count / 行数 で計算されます。unique_rate = 1 であれば、全ての行に異なる値が入っています。一般的に、実数値の列は unique_rate が高くなりますが、年齢の「20代」や価格の「200円代」のように階級に分けられている場合には unique_rate が低くなります。

使用例 Examples

import py4stats as py4st
from palmerpenguins import load_penguins
penguins = load_penguins() # サンプルデータの読み込み

print(py4st.diagnose(penguins).round(4))
#>              columns    dtype  missing_count  missing_percent  unique_count  unique_rate
#> 0            species   object              0           0.0000             3       0.8721
#> 1             island   object              0           0.0000             3       0.8721
#> 2     bill_length_mm  float64              2           0.5814           165      47.9651
#> 3      bill_depth_mm  float64              2           0.5814            81      23.5465
#> 4  flipper_length_mm  float64              2           0.5814            56      16.2791
#> 5        body_mass_g  float64              2           0.5814            95      27.6163
#> 6                sex   object             11           3.1977             3       0.8721
#> 7               year    int64              0           0.0000             3       0.8721

Return to Function reference.