比较两个熊猫数据帧中的值,与顺序无关

Piyush S.Wanare

我是数据科学的新手。我想检查一个数据帧中的哪些元素存在于另一数据帧中,例如

df1 = [1,2,8,6]
df2 = [5,2,6,9]

# for 1 output should be False

# for 2 output should be True

# for 6 output should be True

等等

注意:我有矩阵而不是向量。

我尝试使用以下代码:

import pandas as pd
import numpy as np

    priority_dataframe = pd.read_excel(prioritylist_file_path, sheet_name='Sheet1', index=None)

    priority_dict = {column: np.array(priority_dataframe[column].dropna(axis=0, how='all').str.lower()) for column in
                         priority_dataframe.columns}
    keys_found_per_sheet = []
    if file_path.lower().endswith(('.csv')):
        file_dataframe = pd.read_csv(file_path)
    else:
        file_dataframe = pd.read_excel(file_path, sheet_name=sheet, index=None)

    file_cell_array = list()
    for column in file_dataframe.columns:
        for file_cell in np.array(file_dataframe[column].dropna(axis=0, how='all')):
            if isinstance(file_cell, str) == 'str':
                file_cell_array.append(file_cell)
            else:
                file_cell_array.append(str(file_cell))

    converted_file_cell_array = np.array(file_cell_array)

    for key, values in priority_dict.items():
        for priority_cell in values:
            if priority_cell in converted_file_cell_array[:]:
                keys_found_per_sheet.append(key)
                break

我做错什么了if priority_cell in converted_file_cell_array[:]吗?

还有其他有效的方法吗?

迪斯

您可以.values从每个数据框中获取,将其转换为set(),然后获取设置的交集。

set1 = set(df1.values.reshape(-1).tolist())
set2 = set(dr2.values.reshape(-1).tolist())
different = set1 & set2

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

比较两个熊猫数据帧中的值以保持运行计数

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

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

如果两个值相等,则求和并折叠熊猫中的两行(顺序无关紧要)

逐行比较两个熊猫数据帧

比较两个熊猫数据帧的行的最快方法?

比较两个熊猫数据帧的行?

通过列值比较两个熊猫数据帧的行

比较两个熊猫数据帧中数据的布尔逻辑

使用 iterrows 进行迭代时比较数据帧中的两个值

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

熊猫在两个具有相同列的数据帧中减去值创建新的数据帧来存储结果

通过忽略熊猫数据框中的秒值来比较两个日期

如何在两个不同的熊猫数据框中相互比较值

比较两个Spark数据帧

基于具有相同对值的两列合并熊猫数据帧,但在两个数据帧中以不同顺序显示

比较两个不同的熊猫列中的值

熊猫-比较两个Csv数据帧,但一个csv出现问题

比较两个熊猫数据框

如何在元素不按顺序排列的两个不同列上合并两个熊猫数据帧?

比较两个熊猫数据帧上的所有列以获得差异

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

比较两个差异熊猫数据框上两列的值并返回最大值

如何基于一行中的值和不同的列名合并两个熊猫数据帧?

如何比较两个熊猫数据帧并删除一个文件上的重复项而又不追加其他文件中的数据

比较两个单独数据帧的“组”中行的“顺序”,找到按顺序“交换”的行并提取原始索引

熊猫数据帧的分类由两个索引值和多索引的数据

如何比较pyspark中两个不同数据帧中的两列

在比较之前将两个数字熊猫数据帧(x,y)与插值进行比较