【Python】Pandas/Dataframeで行数・列数・全要素数を取得する方法
pandas.DataFrame,の行数、列数、全要素数(サイズ)を取得する方法を書き留めておきます。
前提として以下のソースコードに続くものとします。
import pandas as pd d = [['岡部 祐二', 14, '男', '050-3888-0689'], ['今関 卓也', 35, '男', '070-0549-9317'], ['弓削 良平', 40, '男', '090-5861-4371'], ['荒木 崇', 44, '男', '090-0495-9588'], ['小川 英樹', 17, '男', '080-0749-9035'], ['森下 浩', 21, '男', '050-7947-2195']] header = ['名前', '年齢', '性別', '携帯番号'] df = pd.DataFrame(data=d, columns=header)
行数・列数を取得: df.shape
print(df.shape) # (6, 4)
行数を取得: len(df)
print(len(df)) # 6
列数を取得: len(df.columns)
print(len(df.columns)) # 4
全要素数(サイズ)を取得: df.size
print(df.size) # 24
スポンサーリンク