我已经检查了这些现有问题:
…但我还没有完全解决这个问题。你知道吗
我正在尝试编写一个模块来匹配字符串,方法是在源和目标上逐步转换字符串,并检查其他匹配项。为了跟踪重复的转换/匹配尝试,我将数据帧用于源、目标和匹配。你知道吗
因此,解决方案的一部分是为尚未匹配的项创建源/目标子集,应用转换,并提取任何匹配结果。所以我的代码是这样的:
import pandas as pd
def trymatch(transformers):
global matches, source, target
# Don't bother doing work if we've already found a match
if matches is not None:
s_ids = matches['id_s'].values
s_inmask = (~source['id'].isin(s_ids))
s = source.loc[s_inmask].copy()
# ... do the same for the target dataframe
else:
s = source
t = target
for transformer in transformers:
# Call the transformations here...
mnew = pd.merge(s, t, on='matchval', suffixes=['_s', '_t'])
if matches is None: matches = mnew
else: matches = matches.append(mnew)
# ----------------------------------------------------------------------------------------------------------------------
source = pd.DataFrame({'id': [1, 2, 3], 'value': ['a', 'b', 'c']})
target = pd.DataFrame({'id': [4, 5, 6], 'value': ['A', 'b', 'd']})
matches = None
trymatch(['t_null'])
trymatch(['t_upper'])
我的挑战来自trymatch函数,如果匹配已经存在,我将创建子集。即使使用.loc索引,Python也在向我抛出带有copywarning的设置。我可以用.copy()去掉它们,就像我在这里展示的那样。。。我认为这是有效的,因为我只需要这个函数的子集的临时副本。你知道吗
这看起来有效吗?我可以用.is\u copy=False来抑制并保存内存吗?你知道吗
有没有一种更为恶作剧的方式来处理这个问题,从而完全回避这个问题?你知道吗
你所写的是有效的。
pandas
在这种情况下抛出SettingsWithCopy
警告,因为它依赖于numpy
数组语义,为了提高效率,数组语义返回数据的视图,而不是副本。pandas
无法本身检测这何时会导致问题,因此它(保守地)只会在好的情况和坏的情况下抛出这个错误。你知道吗您可以使用以下方法消除错误消息:
有关详细信息,请参见How to deal with SettingWithCopyWarning in Pandas?
相关问题 更多 >
编程相关推荐