【发布时间】:2020-01-16 22:19:01
【问题描述】:
我的目标是找到与general_text 列中的行匹配的City,但匹配必须准确。
我尝试使用搜索IN,但它没有给我预期的结果,所以我尝试使用str.contain,但我尝试这样做的方式向我显示了一个错误。有关如何正确或高效地执行此操作的任何提示?
df['matched'] = df.apply(lambda x: x.City in x.general_text, axis=1)
但它给了我以下结果:
data = [['palm springs john smith':'spring'],
['palm springs john smith':'palm springs'],
['palm springs john smith':'smith'],
['hamptons amagansett':'amagansett'],
['hamptons amagansett':'hampton'],
['hamptons amagansett':'gans'],
['edward riverwoods lake':'wood'],
['edward riverwoods lake':'riverwoods']]
df = pd.DataFrame(data, columns = [ 'general_text':'City'])
df['match'] = df.apply(lambda x: x['general_text'].str.contain(
x.['City']), axis = 1)
我想通过上面的代码收到的是只匹配这个:
data = [['palm springs john smith':'palm springs'],
['hamptons amagansett':'amagansett'],
['edward riverwoods lake':'riverwoods']]
【问题讨论】:
标签: python pandas dataframe row contains