3.Pandas的数据结构和切片及数据清洗


第二章 Python学习入门之pandas的使用


前言

        Pandas 以其灵活的数据结构和强大的处理能力,成为 Python 数据分析的核心工具。无论是数据清洗、特征工程,还是统计分析和可视化,Pandas 都提供了高效且易用的解决方案,是数据科学家的必备工具。


一、pandas是什么?

        Pandas 是 Python 中用于数据处理和分析的核心库,基于 NumPy 构建,提供高效、灵活的数据结构和工具。

二、 核心功能

Series 一维带标签数组,支持自动对齐和缺失值处理。
DataFrame 二维表格型数据结构,含行索引(index)和列索引(columns),支持异构数据。

2.1 核心数据结构Series及创建方法

        在 Pandas 中,Series是一维带标签的数组,可存储任意数据类型(整数、字符串、浮点数等)。其核心特点是索引与值的映射关系,支持自动对齐和缺失值处理。以下是创建Series的主要方式:

2.1.1  从列表或数组创建

import pandas as pd
import numpy as np

# 从列表创建(默认整数索引)
s1 = pd.Series([10, 20, 30, 40])
print(s1)
# 输出:
# 0    10
# 1    20
# 2    30
# 3    40
# dtype: int64

# 指定索引
s2 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s2)
# 输出:
# a    10
# b    20
# c    30
# dtype: int64

# 从NumPy数组创建
arr = np.array([1.5, 2.5, 3.5])
s3 = pd.Series(arr, index=['x', 'y', 'z'])
print(s3)
# 输出:
# x    1.5
# y    2.5
# z    3.5
# dtype: float64

2.1.2 从字典创建

data = {'apple': 5, 'banana': 3, 'cherry': 7}
s4 = pd.Series(data)
print(s4)
# 输出:
# apple     5
# banana    3
# cherry    7
# dtype: int64

# 指定额外索引(缺失值用NaN填充)
s5 = pd.Series(data, index=['apple', 'banana', 'grape'])
print(s5)
# 输出:
# apple     5.0
# banana    3.0
# grape     NaN
# dtype: float64

2.1.3 从标量值创建

标量值会被广播到每个索引位置:

# 所有索引对应相同的值
s6 = pd.Series(100, index=['a', 'b', 'c', 'd'])
print(s6)
# 输出:
# a    100
# b    100
# c    100
# d    100
# dtype: int64

2.1.4  创建含缺失值的 Series

使用np.nan表示缺失值:

s7 = pd.Series([1, np.nan, 3, None], index=['x', 'y', 'z', 'w'])
print(s7)
# 输出:
# x    1.0
# y    NaN
# z    3.0
# w    NaN
# dtype: float64

2.1.5  从函数或生成器创建

通过pd.Series()结合生成器表达式:

# 使用range生成索引和值
s8 = pd.Series(range(1, 5), index=list('abcd'))
print(s8)
# 输出:
# a    1
# b    2
# c    3
# d    4
# dtype: int64

2.1.6 指定数据类型(dtype)

通过dtype参数显式指定数据类型:

s9 = pd.Series([1, 2, 3], dtype='float32')
print(s9.dtype)  # 输出: float32

# 字符串数据默认类型为object
s10 = pd.Series(['a', 'b', 'c'])
print(s10.dtype)  # 输出: object

# 强制转换为字符串类型
s11 = pd.Series(['a', 'b', 'c'], dtype='string')
print(s11.dtype)  # 输出: string

关键属性

创建后,可通过以下属性访问Series的元数据:

s = pd.Series([10, 20, 30], index=['x', 'y', 'z'])

print(s.index)    # 索引:Index(['x', 'y', 'z'], dtype='object')
print(s.values)   # 值:array([10, 20, 30])
print(s.dtype)    # 数据类型:int64
print(s.name)     # 名称:None(可通过name参数设置)

2.2 核心数据结构DataFrame及创建方法

        Pandas 的DataFrame是二维表格型数据结构,由行索引(index)和列索引(columns)定义,可存储不同类型的数据(整数、浮点数、字符串等)。它是数据分析中最常用的工具之一,提供了强大的数据操作和处理能力。

2.2.1 从字典列表创建

import pandas as pd

data = [
    {'name': 'Alice', 'age': 25, 'city': 'New York'},
    {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'},
    {'name': 'Charlie', 'age': 35}  # 缺失值自动填充为NaN
]

df = pd.DataFrame(data)
print(df)
# 输出:
#     name  age         city
# 0  Alice   25     New York
# 1    Bob   30  Los Angeles
# 2  Charlie  35         NaN

2.2.2 从字典创建(列名作为键)

data = {
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35],
    'city': ['New York', 'Los Angeles', None]
}

df = pd.DataFrame(data)
print(df)

2.2.3 从 Series 创建

name = pd.Series(['Alice', 'Bob', 'Charlie'], index=[0, 1, 2])
age = pd.Series([25, 30, 35], index=[0, 1, 2])
city = pd.Series(['New York', 'Los Angeles'], index=[0, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寄思~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值