【发布时间】:2018-04-03 06:02:09
【问题描述】:
我是数据科学的新手。我想检查一个数据框中的哪些元素存在于另一个数据框中,例如
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[:] 做错了什么?
还有其他有效的方法吗?
【问题讨论】:
-
你能添加一些数据样本和预期的输出吗?我认为mcve
-
@JaredWilber ,并不是因为,我想检查一个数据帧的每个元素是否存在到另一个数据帧中。
-
换句话说,您想检查两个数据帧是否具有完全相同的元素,但位置无关紧要,对吧?
-
我的错。我认为你应该进一步澄清这个问题,我仍然对你在问什么感到困惑。
标签: python python-3.x pandas unordered