如何獲取 Pandas DataFrame 的行數?


若要獲取 Pandas DataFrame 的行數,我們可以使用 DataFrame 索引長度。

步驟

  • 建立一個二維可變大小的異構表格資料,即**df**。

  • 列印輸入的 DataFrame。

  • 列印 DataFrame 索引列表的長度,即 **len(df.index)**。

示例

 線上演示

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 1, 9],
      "y": [4, 1, 5, 10],
      "z": [4, 1, 5, 0]
   }
)

print "Input DataFrame is:
", df print "Row count of DataFrame is: ", len(df.index)

輸出

Input DataFrame is:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
3  9 10  0
Row count of DataFrame is: 4

更新時間:2021-08-30

441 次檢視

開啟你的 職業生涯

完成課程認證

開始
廣告