大熊猫:比较来自两个不同大小的不同数据帧的字符串列

扎拉·科拉加

我有两个大小不同的数据框,每个数据框都有一列句子,如下所示:

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')

但是当我检查时,它只会返回false,而不会返回“ true”

耶斯列尔

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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

计算来自两个不同数据帧的两个字符串列之间的编辑距离

值在大熊猫比较数据帧被索引的不同

比较来自不同数据帧的两个值,并基于熊猫中的添加值

比较两个大小不同的熊猫数据框

比较来自两个不同 pyspark 数据帧的两对列以显示不同的数据

如何创建包含在数据帧大熊猫基于同一类别列的两个不同的值的操作的另一列?

比较两个数据框列以匹配字符串或子字符串,然后以大熊猫计数

两个如何比较来自两个不同数据框的熊猫的两行

大熊猫映射来自具有不同列名的2个数据框的数据

来自两个不同数据帧的列之间的成对比较

应用具有来自两个不同数据帧的参数的函数,并使用 group by。(熊猫)

将两个数据框与大熊猫合并后的行数不同

大熊猫-以正确的方式连接两个不同结构的JSON

比较两个熊猫数据帧的内容,即使行的顺序不同

如何比较基于列的两个不同大小的数据帧?

如何比较来自两个不同数据帧的两个句子的函数中的处理时间减少?

仅某些行的两个大小不同的熊猫数据帧的列总和

字符串包含两个大熊猫系列

如何加入两个大熊猫数据框

使用R比较两个不同行的数据帧

如何从两个不同的熊猫数据帧计算比率

找出熊猫中两个不同数据帧的交集

如何使用python比较来自两个不同文件的行字符串?

添加两个不同大小和子集的数据帧

使用 Django 比较来自两个不同模型的数据

比较两个不同字符串中的字符

串联两个大小不同的数据框(熊猫)

熊猫比较两个不同长度的数据帧并将某些行分成两半

如何不同的功能,适用于在大熊猫数据帧不同的列