gpt4 book ai didi

python - 从 pandas 数据框中识别相似数据集中的第一行

转载 作者:行者123 更新时间:2023-12-04 02:31:31 26 4
gpt4 key购买 nike

我有一个类似于下图的数据框:

    BillNumber Description            LineAmount   TotalAmount
0 INV001 Line Item 1 of INV001 500 700
1 INV001 Line Item 2 of INV001 200 700
2 INV002 Line Item 1 of INV002 100 800
3 INV002 Line Item 2 of INV002 300 800
4 INV002 Line Item 3 of INV002 400 800

我想要的是这样的:

    BillNumber Description            LineAmount   TotalAmount  NewBill
0 INV001 Line Item 1 of INV001 500 700 Yes
1 INV001 Line Item 2 of INV001 200 700
2 INV002 Line Item 1 of INV002 100 800 Yes
3 INV002 Line Item 2 of INV002 300 800
4 INV002 Line Item 3 of INV002 400 800

我想识别每个新 BillNumber 的第一行,并在名为“NewBill”的新列下为它标记值“Yes”。我们如何使用 pandas 实现这一目标?

提前致谢。

最佳答案

使用numpy.whereSeries.duplicated :

df['NewBill'] = np.where(df['BillNumber'].duplicated(), '', 'Yes')
print (df)
BillNumber Description LineAmount TotalAmount NewBill
0 INV001 Line Item 1 of INV001 500 700 Yes
1 INV001 Line Item 2 of INV001 200 700
2 INV002 Line Item 1 of INV002 100 800 Yes
3 INV002 Line Item 2 of INV002 300 800
4 INV002 Line Item 3 of INV002 400 800

关于python - 从 pandas 数据框中识别相似数据集中的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63920408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com