熊猫使用具有重复索引的另一个数据框列的单元格值更新一列

时间:2020-08-08 05:58:30

标签: python pandas dataframe

有2个dfs

df = pd.DataFrame({'A': ['a', 'b', 'a','d','e']},index=[1,2,3,4,5])

ndf = pd.DataFrame({'A': ['a', '2', '6','e'],
                   'B': ['apple', 'pen', 'sky','duck']},index=[7,8,9,19])

df的A列应使用ndf的B列值进行更新,如下所示: 如果df'A列的值的单元格== ndf's A列的值,则df'A列的单元格值不更新; 除此以外: df的A列单元格值应替换为ndf的B列单元格值:

例如: 更新后,A应该像这样:

pd.DataFrame({'A': ['apple', 'b', 'apple','d','duck']},index=[1,2,3,4,5])

2 个答案:

答案 0 :(得分:3)

您可以使用declare @tmp table (TestCaseID int, currentResultVerdict varchar(50)) insert into @tmp values (1, 'Passed') ,(2, 'Failed') ,(3, 'Blocked') ,(4, 'Blocked') ,(5, 'Inprogress') ,(6, 'Passed') ,(7, 'Passed') select sum (case when currentResultVerdict='Passed' then 1 else 0 end) as Passed ,sum (case when currentResultVerdict='Failed' then 1 else 0 end) as Failed ,sum (case when currentResultVerdict='Blocked' then 1 else 0 end) as Blocked ,sum (case when currentResultVerdict='Inprogress' then 1 else 0 end) as Inprogress from @tmp 创建字典,然后使用to_dict

replace

输出:

df.replace(ndf.set_index('A').to_dict()['B'])

详细信息

       A
1  apple
2      b
3  apple
4      d
5   duck

答案 1 :(得分:1)

dict创建ndf,将其映射到df。将结果用于combine_firstdf.A

df.A.map(dict(zip(ndf.A,ndf.B))).combine_first(df.A)

1    apple
2        b
3    apple
4        d
5     duck

Step by Step

`d= dict(zip(ndf.A,ndf.B))`#Create dict

df['C']= df.A.map(d)# map dict to df.A

df.A=df.C.combine_first(df.A)#Update df.A