目录
一、实战场景
实战场景:Pandas如何从股票数据找出收盘价最低行
二、主要知识点
三、菜鸟实战
马上安排!
1、创建 python 文件
"""
数据是CSV格式
1、加载到dataframe
2、找出收盘价最低的索引
3、根据索引找出数据行4 打印结果数据行
"""
import pandas as pd
df = pd.read_csv("./00700.HK.csv")
df["Date"] = pd.to_datetime(df["Date"])
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
print(df)
print(df.groupby("Year")["Close"].mean())
print(df.describe())
2、运行结果
Date Open High Low Close Volume Year Month
0 2021-09-30 456.000 464.600 453.800 461.400 17335451 2021 9
1 2021-09-29 461.600 465.000 450.200 465.000 18250450 2021 9
2 2021-09-28 467.000 476.200 464.600 469.800 20947276 2021 9
3 2021-09-27 459.000 473.000 455.200 464.600 17966998 2021 9
4 2021-09-24 461.400 473.400 456.200 460.200 16656914 2021 9
... ... ... ... ... ... ... ... ...
4262 2004-06-23 4.050 4.450 4.025 4.425 55016000 2004 6
4263 2004-06-21 4.125 4.125 3.950 4.000 22817000 2004 6
4264 2004-06-18 4.200 4.250 3.950 4.025 36598000 2004 6
4265 2004-06-17 4.150 4.375 4.125 4.225 83801500 2004 6
4266 2004-06-16 4.375 4.625 4.075 4.150 439775000 2004 6
[4267 rows x 8 columns]
Year
2004 4.338686
2005 6.568927
2006 15.865951
2007 37.882724
2008 54.818367
2009 96.369679
2010 157.299598
2011 189.737398
2012 228.987045
2013 337.136066
2014 271.291498
2015 144.824291
2016 176.562041
2017 291.066667
2018 372.678862
2019 346.225203
2020 479.141129
2021 586.649189
Name: Close, dtype: float64
菜鸟实战,持续学习!