Python可视化数据分析06、Pandas进阶
Python可视化数据分析06、Pandas进阶
📋前言📋
💝博客:【红目香薰的博客_CSDN博客-计算机理论,2022年蓝桥杯,MySQL领域博主】💝
✍本文由在下【红目香薰】原创,首发于CSDN✍
🤗2022年最大愿望:【服务百万技术人次】🤗
💝Python初始环境地址:【Python可视化数据分析01、python环境搭建】💝
环境需求
环境:win10
开发工具:PyCharm Community Edition 2021.2
数据库:MySQL5.6
目录
pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
pip3 config list
pip3 install --upgrade pip
pip3 install requests
pip3 install pandas
datetime对象
时间序列数据是一种重要的结构化数据形式。
在Python语言中,datetime模块中的datetime、time和calendar等类都可以用来存储时间类型及进行一些转换和运算操作
datetime对象的常用操作如下:
datetime对象间的减法运算会得到一个timedelta对象,timedelta对象代表两个时间之间的时间差
datetime对象与它所保存的字符串格式时间戳之间可以互相转换。
import datetime
n = datetime.datetime.now()
# str(time)函数返回字符串格式时间戳
print(str(n))
# time.strftime(format)函数返回以可读字符串表示的当地时间,格式由format决定
print(n.strftime("%Y-%m-%d"))
# time.strptime(string, format)函数根据format指定的格式,把一个时间字符串string解析为时间
print(datetime.datetime.strptime("2022-7-27 19:19:17", "%Y-%m-%d %H:%M:%S"))
时间序列
Pandas最基本的时间日期对象是一个从Series派生出来的子类TimeStamp。
Pandas最基本的时间序列类型就是以时间戳(TimeStamp)为index元素的Series类型。
时间序列只是index比较特殊的Series,因此一般的索引操作对时间序列依然有效。
import datetime as datetime
import pandas as pd
import numpy as np
from pandas import Series
print("当前时间:", pd.to_datetime(datetime.datetime.now()))
dates = [datetime.datetime(2022, 1, 1), datetime.datetime(2022, 1, 2), datetime.datetime(2022, 12, 31)]
ts = Series(np.random.rand(3), index=dates)
print("------------------")
print(ts)
# 普通索引操作
print("------------------")
print("下标:", ts.index)
print("------------------")
print("下标[2]:", ts.index[2])
print("------------------")
# 使用各种字符串进行索引
print(ts["20220101"])
print("--------20220101----------")
print(ts["2022-01-01"])
print("--------2022-01-01----------")
print(ts["2022"])
print("--------2022----------")
print(ts["2022-1-1":"2022-1-5"]) # 范围
print("--------2022-1-1 2022-1-5----------")
date_range()
参数值 | 说明 |
Y | 年 |
M | 月 |
D | 日 |
H | 小时 |
T | 分钟 |
S | 秒 |
import pandas as pd
print(pd.date_range("20220101", "20220110"))
print(pd.date_range(start="20220101", periods=10))
print(pd.date_range(end="20220110", periods=10))
print(pd.date_range("20220101", "20220601", freq="M"))
print(pd.date_range('20220918', '2022-09-28'))
print(pd.date_range('2022/09/18', '2022-09-28'))
print(pd.date_range('2022/9/18', '2022-9-19', freq="3H"))
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
'2022-05-31'],
dtype='datetime64[ns]', freq='M')
DatetimeIndex(['2022-09-18', '2022-09-19', '2022-09-20', '2022-09-21',
'2022-09-22', '2022-09-23', '2022-09-24', '2022-09-25',
'2022-09-26', '2022-09-27', '2022-09-28'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-09-18', '2022-09-19', '2022-09-20', '2022-09-21',
'2022-09-22', '2022-09-23', '2022-09-24', '2022-09-25',
'2022-09-26', '2022-09-27', '2022-09-28'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-09-18 00:00:00', '2022-09-18 03:00:00',
'2022-09-18 06:00:00', '2022-09-18 09:00:00',
'2022-09-18 12:00:00', '2022-09-18 15:00:00',
'2022-09-18 18:00:00', '2022-09-18 21:00:00',
'2022-09-19 00:00:00'],
dtype='datetime64[ns]', freq='3H')Process finished with exit code 0
相关文章
- python skitlearn_Python sklearn
- Python数据分析之Pandas(二)
- Python数据分析之Pandas(五)
- python整除和取余写法_Python的整除和取余[通俗易懂]
- python安装不了whl文件_Python安装whl文件过程图解
- python 列表函数
- java和python哪个值得学-学java好还是Python好?
- gyp ERR! stack Error: Can't find Python executable 'python'
- python pkl文件_Python字符串格式化输出的方式包括
- python分段线性插值_Python实现分段线性插值
- 2022 IDE各种激活的,码上用起来吧 ,java、python、php、go等等
- Python自动化办公--Pandas玩转Excel数据分析【二】
- Python IDE开发环境PyCharm中文版,PyCharm软件安装激活教程下载
- Linux上的Python之旅(linux自带python)
- Python输出函数print()总结(python print())详解编程语言
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- 使用Python连接SQL Server数据库(python连接sqlserver)
- python从MSSQL到Python:一段跨语言的旅程(mssql除以)
- Python实现MySQL数据库的读取(python读取mysql)
- Python爬取MySQL数据,助力数据分析(python读取mysql数据)
- Python中使用MySQL数据库的操作技巧(mysql_python)
- Python和Ruby中each循环引用变量问题(一个隐秘BUG?)