У меня есть два фрейма данных разных размеров, и у каждого есть столбец предложений, как показано ниже:
import pandas as pd
data1 = {'text': ['the old man is here','the young girl is there', 'the old woman is here','the young boy is there','the young girl is here','the old girl is here']}
df1 = pd.DataFrame (data, columns = ['text'])
и второй фрейм данных:
data2 = {'text': ['the old man is here','the old girl is there', 'the young woman is here','the young boy is there']}
df2 = pd.DataFrame (data, columns = ['text'])
так что, как вы можете видеть, в обоих фреймах данных есть несколько похожих предложений. то, что я хотел бы получить в качестве вывода, - это столбец в df1, который будет указывать true, если две строки похожи, и false в противном случае:
desired output:
text result
'the old man is here' True
'the young girl is there' False
'the old woman is here' False
'the young boy is there' True
'the young girl is here' False
'the old girl is here' False
Я пытался:
df1['result'] = np.where(df1['text'].str == df2['text'].str, 'True', 'False')
но когда я проверяю, он возвращает только ложь и не «истину»
Используйте Series.isin, если нужно логическое значение True/False
:
df1['result'] = df1['text'].isin(df2['text'])
print (df1)
text result
0 the old man is here True
1 the young girl is there False
2 the old woman is here False
3 the young boy is there True
4 the young girl is here False
5 the old girl is here False
работает как:
#removed '' from 'True', 'False' for boolean
df1['result'] = np.where(df1['text'].isin(df2['text']), True, False)
Ваше решение создает строки, поэтому, если вам нужно использовать его для фильтрации, это не удастся:
df1['result'] = np.where(df1['text'].isin(df2['text']), 'True', 'False')