首页 > 技术文章 > pandas 获取数据帧DataFrame的行、列数

HL-space 2019-04-04 17:22 原文

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()方法。

 

推荐阅读