Python 3.x 熊猫应用、地图和iErrors行为异常
我正在尝试根据另一个列表中的文本修剪列表中的文本。当直接在两个列表上调用时,以下函数可以正常工作Python 3.x 熊猫应用、地图和iErrors行为异常,python-3.x,pandas,Python 3.x,Pandas,我正在尝试根据另一个列表中的文本修剪列表中的文本。当直接在两个列表上调用时,以下函数可以正常工作 def remove_texts(texts, texts2): to_remove = [] for i in texts2: if i in texts: to_remove.append(i) texts = [j for j in texts if j not in to_remove] return texts 但是,下面的代码没有任何作用,我没有得到任何错误 d
def remove_texts(texts, texts2):
to_remove = []
for i in texts2:
if i in texts:
to_remove.append(i)
texts = [j for j in texts if j not in to_remove]
return texts
但是,下面的代码没有任何作用,我没有得到任何错误
df_other.texts = df_other.texts.map(lambda x: remove_texts(x, df_other.to_remove_split))
以下情况也不例外。同样,没有返回任何错误
for i, row in df_other.iterrows():
row['texts'] = remove_texts(row['texts'], row['to_remove_split'])
任何想法都值得赞赏 您实际上想要找到
文本之间的设置差异
和texts2
。假设它们包含:
texts = [ 'AAA', 'BBB', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH' ]
texts2 = [ 'CCC', 'EEE' ]
然后,shortes的解决方案是只计算集合差,
不使用熊猫:
给出:
{'AAA', 'BBB', 'DDD', 'FFF', 'GGG', 'HHH'}
或者,如果您只需要一个列表(未设置),请编写:
如果出于某种原因你想使用熊猫,那么从
两个数据帧的creting:
df = pd.DataFrame(texts, columns=['texts'])
df2 = pd.DataFrame(texts2, columns=['texts'])
然后,您可以计算集合差,如下所示:
df.query('texts not in @df2.texts')
或
df = pd.DataFrame(texts, columns=['texts'])
df2 = pd.DataFrame(texts2, columns=['texts'])
df.query('texts not in @df2.texts')
df.texts[~df.texts.isin(df2.texts)]