<aside> 💡

查看全集:Quantopia量化分析56讲

</aside>


1. Pandas简介与安装

Pandas是Python的数据分析核心库,提供高效的SeriesDataFrame数据结构,专为处理结构化数据设计。

安装

pip install pandas numpy matplotlib

基础导入

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

2. Series:一维数据处理

2.1 创建与属性

# 从列表创建Series
s = pd.Series([1, 2, np.nan, 4, 5], name="Toy Series")
print(s)

输出:

0    1.0
1    2.0
2    NaN
3    4.0
4    5.0
Name: Toy Series, dtype: float64

设置索引

s.index = pd.date_range("2023-01-01", periods=5, freq="D")
print(s.index)

输出:

DatetimeIndex(['2023-01-01', '2023-01-02', ..., '2023-01-05'], dtype='datetime64[ns]', freq='D')


2.2 索引操作