处理数据部分必备代码

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)
相关推荐
SEO_juper10 小时前
外贸多语言站最隐蔽的流量杀手:hreflang 错了,谷歌把德语页推给美国人(附审计脚本)
开发语言·前端·python·seo·独立站·谷歌优化
Metaphor69210 小时前
快速合并 Word 文档【Python 指南】
python·word
JAVA老架构AI智能化10 小时前
如何高质量分块
人工智能·python
泡海椒10 小时前
告别反射低效:JQuick-Java ASM动态调用链性能优化实战
java·python·性能优化
2601_9563198810 小时前
用示例和练习,把量化规则先练清楚
人工智能·python
满怀冰雪11 小时前
25-迁移学习入门:加载预训练模型并微调
人工智能·python·机器学习·迁移学习·paddle
2601_9622845011 小时前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_9620780311 小时前
构建RESTful APIs:使用Python和Flask
python·flask·web开发·api设计·构建restfulapis
半亩码田11 小时前
C#转Python第4.5篇:单元测试:pytest vs xUnit/NUnit
python·单元测试·c#
2601_9623008111 小时前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests