【发布时间】:2021-03-24 11:59:23
【问题描述】:
我有两个不同的数据框,A 和 B,它们有一个列,每个列都有一个字符串。我的目标是遍历 A 中的字符串列并检查 B 中是否存在该字符串,如果不存在,则对 A 中的其他列进行一些计算,然后使用 A 中的值向 B 写入一个新行。我很难检查字符串相似度。
我试过这个:
if A.string1.isin(B.string2.any() :
我得到一个 TypeError: only list-like objects are allowed to be passed to isin(), you pass a [str]
我也尝试过在 A 上使用 for 循环:
for value in A.itertuples() :
if value.string1.isin(B.string2.any()) :
然后我得到 AttributeError: 'str' object has no attribute 'isin'
样本数据:
A = pd.DataFrame({'A': ["john doe", " john doe", 'John'], 'B': [6, 7, 8]})
B = pd.DataFrame({'C': ["john dow", " john dough", 'john doe'], 'D': [9, 10, 11]})
有什么想法吗?
【问题讨论】:
-
能否提供一个示例数据
-
当然!
A = pd.DataFrame({'A': ["john doe", " john doe", 'John'], 'B': [6, 7, 8]})B = pd.DataFrame({'C': ["john dow", " john dough", 'John doe'], 'D': [9, 10, 11]})
标签: python pandas string for-loop isin