pandasは、データフレームの行、列、および要素の数を取得します

プロジェクトのgithubアドレス:bitcarmanlee easy-algorithm-interview-and-practiceには、
多くの場合、学生にプライベートメッセージを送信したり、関連する質問をするためのメッセージを残したりします。V番号bitcarmanlee。githubのスターのクラスメートは、私の能力と時間の範囲内で、あなたが関連する質問に答え、一緒に進歩するのを助けるために最善を尽くします。

1.テストデータ

1457822940      0       0       44      36
422692440       0       0       3       3
1460826600      0       0       7       6
1410115140      -1      3       25      7
1161370800      0       0       18      14
996746700       0       0       30      25
1115896320      0       0       441     123
64954980        0       0       7       7
2307334696      0       0       2       2
417770700       0       0       1       1

2.行、列、要素などの数を取得します。

def test():
        names = ['c1', 'c2', 'c3', 'c4', 'c5']
        df = pd.read_csv("testdata", sep="\t", header=None, names=names)
        print(df.info())
        print("\n")
        print("len is: ", len(df))
        print("columns is: ", df.columns)
        print("shape is: ", df.shape)
        print("size is: ", df.size)

コード出力結果:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   c1      10 non-null     int64
 1   c2      10 non-null     int64
 2   c3      10 non-null     int64
 3   c4      10 non-null     int64
 4   c5      10 non-null     int64
dtypes: int64(5)
memory usage: 528.0 bytes
None


len is:  10
columns is:  Index(['c1', 'c2', 'c3', 'c4', 'c5'], dtype='object')
shape is:  (10, 5)
size is:  50

その中で、info()メソッドには、タイプ、列情報、null以外の数、データ型、メモリ使用量など、多くの情報が含まれています。

len(df)は、データフレーム
列の行数を取得できます。列の
形状に関連する情報は、行と列の情報を含む2タプルであるため、データフレームの行と列のデータを取得する場合は、あなたは形を通してそれを得ることができます。
sizeは、データフレーム全体の要素の数です。

おすすめ

転載: blog.csdn.net/bitcarmanlee/article/details/112376886