pandasで行数、列数、全要素数(サイズ)を取得
pandas.DataFrame, pandas.Seriesの行数、列数、全要素数(サイズ)をカウントし取得する方法を示す。
目次
例としてタイタニックの生存者のデータを使用する。Kaggleの問題からダウンロードできる。
import pandas as pd
print(pd.__version__)
# 2.0.0
df = pd.read_csv('data/src/titanic_train.csv')
print(df.head())
# PassengerId Survived Pclass
# 0 1 0 3 \
# 1 2 1 1
# 2 3 1 3
# 3 4 1 1
# 4 5 0 3
#
# Name Sex Age SibSp
# 0 Braund, Mr. Owen Harris male 22.0 1 \
# 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
# 2 Heikkinen, Miss. Laina female 26.0 0
# 3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
# 4 Allen, Mr. William Henry male 35.0 0
#
# Parch Ticket Fare Cabin Embarked
# 0 0 A/5 21171 7.2500 NaN S
# 1 0 PC 17599 71.2833 C85 C
# 2 0 STON/O2. 3101282 7.9250 NaN S
# 3 0 113803 53.1000 C123 S
# 4 0 373450 8.0500 NaN S
source: pandas_len_shape_size.py
pandas.DataFrameの行数・列数・全要素数(サイズ)を取得
行数・列数などを表示: df.info()
pandas.DataFrameのinfo()メソッドで、行数・列数や全体のメモリ使用量、各列のデータ型や欠損値ではない要素の数などの情報を表示できる。
df.info()
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 891 entries, 0 to 890
# Data columns (total 12 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 PassengerId 891 non-null int64
# 1 Survived 891 non-null int64
# 2 Pclass 891 non-null int64
# 3 Name 891 non-null object
# 4 Sex 891 non-null object
# 5 Age 714 non-null float64
# 6 SibSp 891 non-null int64
# 7 Parch 891 non-null int64
# 8 Ticket 891 non-null object
# 9 Fare 891 non-null float64
# 10 Cabin 204 non-null object
# 11 Embarked 889 non-null object
# dtypes: float64(2), int64(5), object(5)
# memory usage: 83.7+ KB
source: pandas_len_shape_size.py
結果は標準出力され、値として取得することはできない。
行数・列数を取得: df.shape
pandas.DataFrameのshape属性で行数と列数のタプル(行数, 列数)を取得できる。
print(df.shape)
# (891, 12)
print(df.shape[0])
# 891
print(df.shape[1])
# 12
source: pandas_len_shape_size.py
アンパックでそれぞれ別の変数に格納することも可能。
row, col = df.shape
print(row)
# 891
print(col)
# 12
source: pandas_len_shape_size.py
行数を取得: len(df)
Pythonの組み込み関数len()でpandas.DataFrameの行数が取得できる。
print(len(df))
# 891
source: pandas_len_shape_size.py
列数を取得: len(df.columns)
pandas.DataFrameの列数はcolumns属性に対してlen()を適用することで取得できる。
print(len(df.columns))
# 12
source: pandas_len_shape_size.py
全要素数(サイズ)を取得: df.size
pandas.DataFrameの全要素数はsize属性で取得できる。これは行数 * 列数の値に等しい。
print(df.size)
# 10692
print(df.shape[0] * df.shape[1])
# 10692
source: pandas_len_shape_size.py
インデックスを指定したときの注意点
set_index()メソッドでデータ列をインデックスとして指定すると、その列はデータ本体(values属性)から取り除かれるので、列数にカウントされない。
df_multiindex = df.set_index(['Sex', 'Pclass', 'Embarked', 'PassengerId'])
print(df_multiindex.shape)
# (891, 8)
print(len(df_multiindex))
# 891
print(len(df_multiindex.columns))
# 8
print(df_multiindex.size)
# 7128
source: pandas_len_shape_size.py
set_index()については以下の記事を参照。
pandas.Seriesの全要素数(サイズ)を取得
pandas.Seriesの例としてpandas.DataFrameから一列抽出する。
s = df['PassengerId']
print(s.head())
# 0 1
# 1 2
# 2 3
# 3 4
# 4 5
# Name: PassengerId, dtype: int64
source: pandas_len_shape_size.py
全要素数(サイズ)を取得: len(s), s.size, s.shape
pandas.Seriesは一次元なので、len()でもsize, shape属性でも全要素数(サイズ)が取得できる。shape属性は要素数が1のタプルとなるので注意。
print(len(s))
# 891
print(s.size)
# 891
print(s.shape)
# (891,)
print(type(s.shape))
# <class 'tuple'>
source: pandas_len_shape_size.py
pandas 1.4でpandas.Seriesにもinfo()メソッドが追加された。
s.info()
# <class 'pandas.core.series.Series'>
# RangeIndex: 891 entries, 0 to 890
# Series name: PassengerId
# Non-Null Count Dtype
# -------------- -----
# 891 non-null int64
# dtypes: int64(1)
# memory usage: 7.1 KB
source: pandas_len_shape_size.py