pandas - Pandas 将年/月 Int 列转换为日期时间和季度平均值
问题描述
我有一个 df 中的数据,该数据分为年和月列,我试图找到观察到的数据列的平均值。我在网上找不到如何将“年”和“月”列转换为日期时间,然后找到 Q1、Q2、Q3 等平均值。
year month data
0 2021 1 7.100427005789888
1 2021 2 7.22523237179488
2 2021 3 8.301528122415217
3 2021 4 6.843885683760697
4 2021 5 6.12365177832918
5 2021 6 6.049659188034206
6 2021 7 5.271174524400343
7 2021 8 5.098493589743587
8 2021 9 6.260155982906011
我需要最终数据看起来像 -
year Quarter Q data
2021 1 7.542395833
2021 2 6.33906555
2021 3 5.543274699
我已经尝试过将“年”和“月”列更改为日期时间,但它给出了一个从年 = 1970 开始的长日期
df.iloc[:, 1:2] = df.iloc[:, 1:2].apply(pd.to_datetime)
year month wind_speed_ms
0 2021 1970-01-01 00:00:00.000000001 7.100427
1 2021 1970-01-01 00:00:00.000000002 7.225232
2 2021 1970-01-01 00:00:00.000000003 8.301528
3 2021 1970-01-01 00:00:00.000000004 6.843886
4 2021 1970-01-01 00:00:00.000000005 6.123652
5 2021 1970-01-01 00:00:00.000000006 6.049659
6 2021 1970-01-01 00:00:00.000000007 5.271175
7 2021 1970-01-01 00:00:00.000000008 5.098494
8 2021 1970-01-01 00:00:00.000000009 6.260156
谢谢,
解决方案
我希望这对你有用
# I created period column combining year and month column
df["period"]=df.apply(lambda x:f"{int(x.year)}-{int(x.month)}",axis=1).apply(pd.to_datetime).dt.to_period('Q')
# I applied groupby to period
df=df.groupby("period").mean().reset_index()
df["Quarter"] = df.period.astype(str).str[-2:]
df = df[["year","Quarter","data"]]
df.rename(columns={"data":"Q data"})
year Quarter Q data
0 2021.0 Q1 7.542396
1 2021.0 Q2 6.339066
2 2021.0 Q3 5.543275
推荐阅读
- windows - 从另一个区域复制 EC2 实例会带来错误的路由表
- formal-verification - TLA+ 中的咖啡罐问题:无法表达任务
- github-pages - 雨果 github 动作 404
- .htaccess - 如何使用 htaccess 从 URL 中查找和替换特定单词
- javascript - 具有前瞻性的正则表达式不像我期望的那样工作
- python - Python Watermarking - 防止图像大小和质量降低
- apache-kafka - Kafka Connect MQTT:是否可以在 MQTT 和 Kafka 主题之间进行 1:1 映射?
- java - 控制器无法映射对象并带来列表结果
- sql - 如何从日期检查日期名称。但是从程序来看,我的日期错误,所以请帮助我
- node.js - nodejs中的参数问题