Philippe Haumesser
Philippe Haumesser

Reputation: 647

Pandas AttributeError: 'str' object has no attribute 'loc'

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

Upvotes: 1

Views: 1527

Answers (1)

SeaBean
SeaBean

Reputation: 23217

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

Upvotes: 1

Related Questions