python - 通过添加另一列来对我的 pandas 数据框中的重复项进行排序

标签 python python-3.x pandas duplicates

我需要在数据框中订购重复项,计数器值为 1、2、3 等。输入数据框如下所示:

         Key    Amount
         xyz     1000
         xyz1    870
         xyz2    1000
         xyz3    1000
         xyz4    650

预期输出为

         Key    Amount  Duplicate_order
         xyz     1000    1
         xyz1    870     1
         xyz2    1000    2
         xyz3    1000    3
         xyz4    650     1

最佳答案

使用cumcount

df['duplicate_order'] = df.groupby('Amount').cumcount()+1

    Key  Amount  duplicate_order
0   xyz    1000                1
1  xyz1     870                1
2  xyz2    1000                2
3  xyz3    1000                3
4  xyz4     650                1

关于python - 通过添加另一列来对我的 pandas 数据框中的重复项进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50748517/

相关文章:

Python单元测试-可能会一直失败直到通过吗?

python - 登录Python?

python - "error: (-215) ssize.area() > 0 in function cv::resize"的大图像上的 OpenCV 调整大小失败

python - 有没有办法忽略日志文件中的输出并在控制台上打印

Python套接字: WinError 10022

python-3.x - "Getting SSLError while trying to ' 获取 ' content using ' 请求 ' from a ' HTTPS ' url"

python - 在 pandas 中循环查询

python - Matplotlib 2.2.2 无法 xkcdify 绘图

python - 查找股票行情中带有句点的股票时,pandas 数据读取器出现错误

python - 输入提示数据帧联合的最佳方法