简体   繁体   English

Pandas AttributeError: 'str' object 没有属性 'loc'

[英]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.我的目标是这样做:如果DF['CustomerId']等于row['CustomerId']则将DF['CustomerId']值更改为row['CustomerId']否则保持原样。

row['CustomerId'] and DF['CustomerId'] should be string. row['CustomerId']DF['CustomerId']应该是字符串。 I know that loc works not with string, but how can I do this with string type?我知道loc不适用于字符串,但是如何使用字符串类型来做到这一点? thanks谢谢

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:您可以通过使用.merge()合并公共CustomerId列上的 2 个数据框,然后使用源自“合并”数据框的code列更新CustomerID列,而不使用.update() ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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