Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一次返回多行时,如何修改Datafram中的单行?_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 一次返回多行时,如何修改Datafram中的单行?

Python 一次返回多行时,如何修改Datafram中的单行?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有一个包含两列的数据框,即事务和状态 预期数据帧: Transaction | Status ------------------------- 57230477 | Completed 57232288 | Completed 57232288 | 57232288 | 57228666 | Completed 57229869 | Completed 57233318 | Completed 57233318 |

我有一个包含两列的数据框,即事务和状态

预期数据帧:

Transaction |   Status
-------------------------
57230477    |   Completed
57232288    |   Completed
57232288    | 
57232288    | 
57228666    |   Completed
57229869    |   Completed
57233318    |   Completed
57233318    |
57227149    |   Completed
57227149    |
57222266    |   Completed
57222266    |
57222266    |
57233319    |   Completed
57233319    |
57230490    |   Completed
我的代码中发生的是:

for txn in df['Transaction'].unique():
    df.loc[df['Transaction'] == txn, 'Status'] = 'Completed'
在本例中,所发生的是,它将所有行中的状态分配为“已完成”

我得到的是:

Transaction |   Status
-------------------------
57230477    |   Completed
57232288    |   Completed
57232288    |   Completed
57232288    |   Completed
57228666    |   Completed
57229869    |   Completed
57233318    |   Completed
57233318    |   Completed
57227149    |   Completed
57227149    |   Completed
57222266    |   Completed
57222266    |   Completed
57222266    |   Completed
57233319    |   Completed
57233319    |   Completed
57230490    |   Completed
因此,我的问题是,我如何只将Status as Completed的值分配给事务的第一次出现(如顶部的预期数据框中),即只将值分配给唯一的事务并跳过重复的事务

例如,57232288重复3次,而不是分配已完成的3次,在第一次出现时只分配一次值。

一种方法是使用drop_duplicates并获取索引,然后直接分配:

df.loc[df.drop_duplicates(keep="first").index, "Status"] = "Completed"
print (df)

    Transaction     Status
0      57230477  Completed
1      57232288  Completed
2      57232288           
3      57232288           
4      57228666  Completed
5      57229869  Completed
6      57233318  Completed
7      57233318           
8      57227149  Completed
9      57227149           
10     57222266  Completed
11     57222266           
12     57222266           
13     57233319  Completed
14     57233319           
15     57230490  Completed
按~英寸与反转遮罩一起使用:

df.loc[~df['Transaction'].duplicated(), "Status"] = "Completed"
print (df)
    Transaction     Status
0      57230477  Completed
1      57232288  Completed
2      57232288        NaN
3      57232288        NaN
4      57228666  Completed
5      57229869  Completed
6      57233318  Completed
7      57233318        NaN
8      57227149  Completed
9      57227149        NaN
10     57222266  Completed
11     57222266        NaN
12     57222266        NaN
13     57233319  Completed
14     57233319        NaN
15     57230490  Completed