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
423 字
1 分钟
笔记|数据分析学习笔记17|DataFrame的介绍和创建

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

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

上一篇:笔记|数据分析学习笔记16|Series案例


17.1 DataFrame 结构图解#

DataFrame 是 Pandas 中的二维表格结构,由两大部分组成:

  1. Index(行索引):每一行称为一条 Record(记录);
  2. 多列 Series:每一列都是一个独立的 Series 对象,各列可拥有不同数据类型。

整体类似 Excel 整张工作表,通过行索引 + 列标签共同定位每一个单元格。

name score age
1 tom 60.5 15
2 tom 60.5 15
3 jack 80.0 15
4 alice 30.6 20
5 bob 70.0 26
6 allen 83.5 30
  • 行索引:1, 2, 3, 4, 5, 6
  • 列标签:name, score, age
  • 每一列(如 score)本质上是一个 Series

17.2 DataFrame 的创建方式#

17.2.1 通过 Series 创建#

import pandas as pd
import numpy as np
s1 = pd.Series([1, 2, 3, 4, 5])
s2 = pd.Series([6, 7, 8, 9, 10])
df = pd.DataFrame({'第一列': s1, '第二列': s2})
print(type(df['第一列'])) # 每一列是一个 Series

输出:

<class 'pandas.core.series.Series'>

将多个 Series 传入字典,键作为列名,值作为列数据。

17.2.2 通过字典创建#

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

输出:

name score age
1 tom 60.5 15
2 tom 60.5 15
3 jack 80.0 15
4 alice 30.6 20
5 bob 70.0 26
6 allen 83.5 30

要点说明:

  • 字典的键 → 列标签(columns
  • index 参数:指定自定义行索引
  • columns 参数:指定列的显示顺序(可调整列的顺序)
  • 各列可以是不同数据类型(name 为字符串、score 为浮点、age 为整数)

17.3 本节小结#

本节完成了以下内容:

  • 理解了 DataFrame 的二维结构:行索引 + 多列 Series。
  • 掌握了两种创建方式:通过 Series 字典创建、通过普通字典配合 indexcolumns 参数创建。
  • 了解了 DataFrame 中每一列本质上是一个独立的 Series 对象。

下一节我们将学习 DataFrame 的属性

下一篇:笔记|数据分析学习笔记18|DataFrame的属性


分享

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

笔记|数据分析学习笔记17|DataFrame的介绍和创建
https://luq-blog.xyz/posts/2026-07-04-data-notes-17-df-intro/
作者
Luquiescene
发布于
2026-07-04
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录