python - 根据某些列中的相似值合并行
问题描述
请我需要帮助,我最近开始学习python。请问,如何合并具有相同“PatientID”和相同“Resource”的行,并将“StartDate”和“EndDate”作为合并行的平均值?
解决方案
Givendf
是pandas.DataFrame
包含您的数据的名称。
要获取每个患者的最早资源StartDate
,EndDate
您可以编写:
# Group by the 'PatientID' and 'Resource' columns
grouped_df = df.groupby(['PatientID', 'Resource'])
# Select Earliest `StartDate` and `EndDate` from aggregate.
grouped_df = grouped_df.min(['StartDate', 'EndDate'])
# Remove levels from the index.
grouped_df.reset_index(inplace=True)
推荐阅读
- shell - 如何使 docker 入口点仅针对某些特定命令以非 root 身份运行
- python - GUI class using functions before class creation
- python - 无法在 Windows 中安装 pycocotools:致命错误 C1083:无法打开包含文件:'io.h':没有此类文件或目录错误:
- ethereum - 在 Truffle 上测试部署的智能合约
- c++ - conditional statement in loop c++
- ajax - Why am I getting 'Symfony2 No route found for "GET /module/%3C" ...', when similar calls work
- mongodb - Formatting MongoDB aggregation for Highcharts Stacked Columns
- html - Can't use column-count in media query
- python-3.x - 触发 lamda 功能时,我无法删除 tmp 文件夹中的图像
- java - Access denied for user 'root'@'localhost' with two DataSources in context.xml