python - Pandas Python:附加数据框 - 文本
问题描述
假设我有以下数据框。如何在同一天附加与同一用户对应的行?Python-熊猫
user date text
A 1-1 how
A 1-1 are
A 3-1 the dog
B 1-2 hi
B 1-2 there
B 3-2 be good
user date text
A 1-1 how are
A 3-1 the dog
B 1-2 hi there
B 3-2 be good
解决方案
您可以使用以下功能pivot_table()
:
df.pivot_table(index=['user', 'date'], values='text', aggfunc=' '.join).reset_index()
结果:
user date text
0 A 1-1 how are
1 A 3-1 the dog
2 B 1-2 hi there
3 B 3-2 be good
推荐阅读
- android - 如何获取下一个 RecyclerView 项目的参考
- c# - EF Core - 按类别名称过滤列表 - 忽略大小写
- c# - 用户关闭时 UWP 重新启动
- bash - 在 entrypoint.sh 中运行 docker 命令时找不到 docker
- file - Serilog - 无法打开文件 - 另一个进程使用的文件错误
- dart - dart - `this` 是否取决于调用点
- javascript - 尝试使用 AJAX 从公共目录中选择随机图像
- kubernetes - 使用 Ghost docker 镜像和 Kubernetes 时如何增加最大文件大小
- postgresql - Postgres 选择查询在使用 JDBC 时运行缓慢,但在从同一服务器在 PSQL 中运行时快速
- node.js - 解构 Sequelize 查询