mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7mobile wallpaper 8mobile wallpaper 9mobile wallpaper 10mobile wallpaper 11mobile wallpaper 12mobile wallpaper 13mobile wallpaper 14mobile wallpaper 15mobile wallpaper 16mobile wallpaper 17mobile wallpaper 18mobile wallpaper 19mobile wallpaper 20mobile wallpaper 21mobile wallpaper 22mobile wallpaper 23
341 字
1 分钟
笔记|数据分析学习笔记18|DataFrame的属性
2026-07-04
2026-07-05

本系列所有文章都放在数据科学标签,总目录如下:

合集|数据分析学习笔记|系列总目录

上一篇:笔记|数据分析学习笔记17|DataFrame的介绍和创建


18.1 DataFrame 属性总览#

属性说明
indexDataFrame 的行索引
columnsDataFrame 的列标签
valuesDataFrame 的值(numpy 数组)
dtypes每一列的数据类型
shapeDataFrame 的形状(行数, 列数)
ndimDataFrame 的维度(固定为 2)
sizeDataFrame 的元素总个数
loc[]显式索引,按行列标签索引或切片
iloc[]隐式索引,按行列位置索引或切片
at[]使用行列标签访问单个元素
iat[]使用行列位置访问单个元素
T行列转置

18.2 属性访问示例#

沿用第 17 节创建的 DataFrame:

import pandas as pd
import numpy as np
df = pd.DataFrame(
{
'name': ['tom', 'tom', 'jack', 'alice', 'bob', 'allen'],
'score': [60.5, 60.5, 80, 30.6, 70, 83.5],
'age': [15, 15, 15, 20, 26, 30]
},
index=[1, 2, 3, 4, 5, 6]
)

基础属性:

print('行索引:', df.index)
print('列标签:', df.columns)
print('值:\n', df.values)
print('维度:', df.ndim)
print('形状:', df.shape)
print('元素个数:', df.size)
print('数据类型:\n', df.dtypes)

输出:

行索引: Index([1, 2, 3, 4, 5, 6], dtype='int64')
列标签: Index(['name', 'score', 'age'], dtype='object')
值:
[['tom' 60.5 15]
['tom' 60.5 15]
['jack' 80.0 15]
['alice' 30.6 20]
['bob' 70.0 26]
['allen' 83.5 30]]
维度: 2
形状: (6, 3)
元素个数: 18
数据类型:
name object
score float64
age int64
dtype: object

行列转置:

print(df.T)
print(df.T.index)

输出:

1 2 3 4 5 6
name tom tom jack alice bob allen
score 60.5 60.5 80.0 30.6 70.0 83.5
age 15 15 15 20 26 30
Index(['name', 'score', 'age'], dtype='object')

转置后原来的行索引变成列标签,列标签变成行索引。


18.3 本节小结#

本节完成了以下内容:

  • 掌握了 DataFrame 的常用属性:indexcolumnsvaluesdtypesshapendimsize
  • 理解了 dtypes(返回每列数据类型)与 dtype(Series 属性)的区别。
  • 学会了通过 T 属性做行列转置。

下一节我们将学习 DataFrame 的访问

下一篇:笔记|数据分析学习笔记19|DataFrame的访问


分享

如果这篇文章对你有帮助,欢迎分享给更多人!

笔记|数据分析学习笔记18|DataFrame的属性
https://luq-blog.xyz/posts/2026-07-04-data-notes-18-df-attributes/
作者
Luquiescene
发布于
2026-07-04
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录