1. 程式人生 > >pandas 獲取數據幀DataFrame的行、列數

pandas 獲取數據幀DataFrame的行、列數

das pre row 技術分享 mage object 獲取數據 inf shape

1、創建數據幀

import pandas as pd
df = pd.DataFrame([[1, ‘A‘, ‘3%‘ ], [2, ‘B‘]], index=[‘row_0‘, ‘row_1‘], columns=[‘col_0‘, ‘col_1‘, ‘col_2‘])

技術分享圖片

2、獲取形狀信息

shape = df.shape

技術分享圖片

2.1 獲取行數

rows = shape[0]

rows = len(df.index)

技術分享圖片

2.2 獲取列數

cols = df.shape[1]

cols = len(df.columns)

技術分享圖片

  其中df.index和df.columns是df的行索引和列索引,類型是‘object’型,可以適用len()方法。

技術分享圖片

pandas 獲取數據幀DataFrame的行、列數