处理数据部分必备代码

1、读取数据出现UTF-8错误

python 复制代码
encoding="gbk"

2、进行时间系列的平均,并将平均后的数据转化为时间格式

python 复制代码
data.index = pd.to_datetime(data.index)
data = data.groupby(data.index.to_period('H')).mean()
data.index = data.index.to_timestamp()
df['hour'] = df.index.hour #提取时间中的小时
df.query('hour >= 9 and hour <= 12') # 选择小时范围在9到12小时内的数据

3、将不同的datafram合并

python 复制代码
pd.concat([df1,df2,df3],axis = 1)
#axis=1按列,axis=0按行

4、补全时间

python 复制代码
t_index = pd.date_range('2022-02-01 00:00:00', '2022-02-28 23:55:00', freq='5T')
df.index = pd.to_datetime(df.index, format='%Y-%m-%d %H:%M:%S')
df = df.reindex(t_index)

5、删除缺失值

python 复制代码
data.dropna(axis=0,how='all') #只删除行中所有为Nan值的行
data.dropna(axis=0,how='any')  # 删除行中有含有任何NaN的行
# axis控制行(0)和列(1)
相关推荐
金銀銅鐵4 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup119 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0011 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵13 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf13 小时前
Agent 流程编排
后端·python·agent
copyer_xyf14 小时前
Agent RAG
后端·python·agent
copyer_xyf14 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf14 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python