在使用字典映射时使用copywarning设置

2024-05-07 23:39:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力理解这个警告:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

我已经看到了很多这样,但我的问题出现时,试图地图。你知道吗

我的代码如下:

def merger(df):

     qdf = pd.read_csv('domains_only_df.csv')

     unfilt_rel_domains = qdf[['name', 'hits', 'owner', 'curated', 'domain']].copy()
     rel_domains = unfilt_rel_domains[unfilt_rel_domains['curated'] == 0]

     hits_dict= pd.Series(rel_domains.hits.values, index=rel_domains.owner).to_dict()

     name_dict = pd.Series(rel_domains.name.values, index=rel_domains.owner).to_dict()

     domain_dict = pd.Series(rel_domains.domain.values, index=rel_domains.owner).to_dict()

     df['Hits'] = df['eid'].map(hits_dict).fillna(0)
     df['Existing_domain'] = df['eid'].map(name_dict).fillna(0)
     df['idn'] = df['eid'].map(domain_dict).fillna(0)

return df

错误发生在.map(),我的问题是如何使用警告建议的使用.loc[row_indexer,col_indexer] = value来编写映射?我需要.map()的速度和查找,但我不太确定如何避免这个警告。你知道吗


Tags: toname警告mapdfvaluedomaindict