简体   繁体   中英

Update column in pandas dataframe based on another column of the same dataframe

I am struggling with updating a dataframe columns. Here is a sample of my dataframe:

data1={'UserId': [1, 2, 3], 'OldAnswer': [4, 4, None]}

df1 = pd.DataFrame.from_dict(data1)

data2={'UserId': [1, 2, 3], 'NewAnswer' : [4, 5, None]}

df2 = pd.DataFrame.from_dict(data2)

merged = pd.merge(df1, df2, on ='UserId', how='outer')

Which gives me:

UserId OldAnswer NewAnswer
1 4 4
2 4 5
3 NaN NaN

Now I Want to update the "OldAnswer" with the "NewAnswer" on rows but when I check the difference between the two columns, it says that on the third row, OldAnswer and NewAnswer are differents. The following code gives me the following result:

merged['OldAnswer'] != merged['NewAnswer']

 > False
 > True    
 > True

I thought I would have been able to update my column by doing this:

i = 0
while i < len(merged):
    if merged['OldAnswer'].iloc[i] != merged['NewAnswer'].iloc[i]:
        merged['OldAnswer'].iloc[i] = merged['NewAnswer'].iloc[i]
        i += 1
    else:
        i += 1

But it doesn't work either.

I feel a bit dumb right now: The simple following code solved it:

merged['OldAnswer'] = merged['NewAnswer']
merged.drop(columns='NewAnswer')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM