Pandas组合两行,只添加一个值

2024-03-29 11:48:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组数据,发现两行需要合并到一个值上。我运行了以下命令:

pd.concat(g for _, g in cityData_df.groupby("city") if len(g) > 1)

并得到以下结果:

        city        driver_count    type
84      Port James  15              Suburban
100     Port James  3               Suburban

如何在cityData_df中合并这两行,使Port James只有一个条目和18个驱动程序


Tags: 数据in命令citydfforlenif