1

this is my code:

DF['CustomerId'] = DF['CustomerId'].apply(str)
print(DF.dtypes)
for index, row in merged.iterrows():
    DF = DF.loc[(DF['CustomerId'] == str(row['CustomerId'])), 'CustomerId'] = row['code']

My goal is to do this: if DF['CustomerId'] is equal to row['CustomerId'] then change value of DF['CustomerId'] to row['CustomerId'] else leave as it is.

row['CustomerId'] and DF['CustomerId'] should be string. I know that loc works not with string, but how can I do this with string type ? thanks

2
  • Are you sure about your "goal"? Commented May 11, 2021 at 11:44
  • Hi, Yes. just want to update customerId as we change of reference method... Commented May 11, 2021 at 11:50

1 Answer 1

1

You can approach without looping by merging the 2 dataframes on the common CustomerId column using .merge() and then update the CustomerID column with the code column originated from the 'merged' datraframe with .update(), as follows:

df_out = DF.merge(merged, on='CustomerId', how='left')
df_out['CustomerId'].update(df_out['code'])

Demo

Data Preparation:

data = {'CustomerId': ['11111', '22222', '33333', '44444'],
 'CustomerInfo': ['Albert', 'Betty', 'Charles', 'Dicky']}
DF = pd.DataFrame(data)

print(DF)

  CustomerId CustomerInfo
0      11111       Albert
1      22222        Betty
2      33333      Charles
3      44444        Dicky


data = {'CustomerId': ['11111', '22222', '44444'],
 'code': ['A1011111', 'A1022222', 'A1044444']}
merged = pd.DataFrame(data) 

print(merged)


  CustomerId      code
0      11111  A1011111
1      22222  A1022222
2      44444  A1044444

Run New Code

# ensure the CustomerId column are strings as you did
DF['CustomerId'] = DF['CustomerId'].astype(str) 
merged['CustomerId'] = merged['CustomerId'].astype(str) 

df_out = DF.merge(merged, on='CustomerId', how='left')


print(df_out)

  CustomerId CustomerInfo      code
0      11111       Albert  A1011111
1      22222        Betty  A1022222
2      33333      Charles       NaN
3      44444        Dicky  A1044444

df_out['CustomerId'].update(df_out['code'])

print(df_out)

# `CustomerId` column updated as required if there are corresponding entries in dataframe `merged`

  CustomerId CustomerInfo      code
0   A1011111       Albert  A1011111
1   A1022222        Betty  A1022222
2      33333      Charles       NaN
3   A1044444        Dicky  A1044444

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.