python - Pandas 基于其他列重新采样列
问题描述
我有一个类似的数据框:
x | y
1 | 1
3 | 1
3 | 1
4 | 1
5 | 2
5 | 2
9 | 2
8 | 2
我想重新采样这个数据帧,以便平均具有相同 y 值的 x 值。换句话说:
x | y
(1+3+3+4)/4 | 1
(5+5+9+8)/4 | 2
我研究了pandas.DataFrame.resample函数,但不确定如何在没有时间戳的情况下执行此操作。
解决方案
以下内容可能会返回您要查找的内容:
import pandas
df = pandas.DataFrame({"x":[1,3,3,4,5,5,9,8],"y":[1,1,1,1,2,2,2,2]})
df.groupby(["y"]).mean().reset_index()
推荐阅读
- hugo - 如何获取 Hugo v0.59 中特定部分的前三个帖子?
- java - 编译后 Java Servlet 无法识别 java 文件中的更改
- python-3.x - 将数据添加到列表的嵌套列表
- mysql - 为 SQL 视图合并两行
- excel - Excel - 文件摘要详细信息?
- connection - 无法在库矩阵中打开文件
- python - 有没有办法验证在 Flask 中选中了一个复选框?
- cytoscape.js - Cytoscape 移除元素布局动画
- python - 使用 Python 中的请求将字符串 id 插入 GET
- python - 从 Python 中的 .docx 文件中的表中提取 Acrobat 文档对象